net的網(wǎng)站建設(shè)(寧波網(wǎng)站建設(shè))

2024-04-30    分類(lèi): 網(wǎng)站建設(shè)

最近有個(gè)客戶(hù)要求開(kāi)發(fā)一套短網(wǎng)址網(wǎng)站,小編現(xiàn)在都使用.net core進(jìn)行網(wǎng)站開(kāi)發(fā)了,以是厘厘思路,想想使用.net core 的中間件應(yīng)該很容易實(shí)現(xiàn)。

1. 構(gòu)建一個(gè)中間件,監(jiān)測(cè)網(wǎng)站的響應(yīng)狀態(tài),代碼如下:

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Http;

using Microsoft.Extensions.Logging;

using NetCoreTFCMS.Domain.DbModel;

using System.Text.RegularExpressions;

using System.Threading.Tasks;

using TianFeng.FrameworkCore.DapperEx;

namespace NetCoreTFCMS.MiddleWare

{

public static class RequestIPExtensions

{

public static IApplicationBuilder UseWatchNoFound(this IApplicationBuilder builder)

{

return builder.UseMiddleware<WatchNoFoundMiddleWare>();

}

}

public class WatchNoFoundMiddleWare

{

private readonly RequestDelegate _next;

private readonly ILogger logger;

public WatchNoFoundMiddleWare(RequestDelegate next, ILoggerFactory loggerFactory)

{

_next = next;

logger = loggerFactory.CreateLogger<WatchNoFoundMiddleWare>();

}

public async Task Invoke(HttpContext context)

{

await _next.Invoke(context);

var path = context.Request.Path.ToString().Trim();

if (path.LastIndexOf("/") == 0)

{

var salt = path.Replace("/", "");

if (Regex.IsMatch(salt, @"^[a-zA-Z0-9]{6}$"))

{

var db = (IDbService)context.RequestServices.GetService(typeof(IDbService));

var model = await db.GetModelAsync<Link>(new { Salt = salt });

if (model != null)

{

if (!Regex.IsMatch(model.SiteUrl, "(file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://", RegexOptions.IgnoreCase))

{

model.SiteUrl = "http://" + model.SiteUrl;

}

context.Response.Redirect(model.SiteUrl, true);

}

else

{

logger.LogInformation(salt + "無(wú)匹配網(wǎng)址");

}

}

}

}

}

}

2.在startup.cs中的 Configure(IApplicationBuilder app)中添加引用 該 中間件

app.UseWatchNoFound();

這樣該中間件就會(huì)響應(yīng)短網(wǎng)址的六位字符串,如果匹配則重定向至對(duì)應(yīng)的網(wǎng)址。

具體的短網(wǎng)址生成,我想就很簡(jiǎn)單,這里就不多說(shuō)了,如果有需要,大家可以咨詢(xún)我。

平臺(tái)界面

輸入網(wǎng)址

自動(dòng)生成當(dāng)前域名的短網(wǎng)址

后臺(tái)管理

非常感謝您讀完創(chuàng)新互聯(lián)的這篇文章:"net的網(wǎng)站建設(shè)(寧波網(wǎng)站建設(shè))",僅為提供更多信息供用戶(hù)參考使用或?yàn)閷W(xué)習(xí)交流的方便。我們公司提供:網(wǎng)站建設(shè)、網(wǎng)站制作、官網(wǎng)建設(shè)、SEO優(yōu)化、小程序制作等服務(wù),歡迎聯(lián)系我們提供您的需求。

網(wǎng)頁(yè)題目:net的網(wǎng)站建設(shè)(寧波網(wǎng)站建設(shè))
標(biāo)題網(wǎng)址:http://www.muchs.cn/news13/326563.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、小程序開(kāi)發(fā)、外貿(mào)建站移動(dòng)網(wǎng)站建設(shè)、Google、用戶(hù)體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)