首页 > 其他 > 详细

开发短网址平台的思路

时间:2018-02-10 14:41:32      阅读:182      评论:0      收藏:0      [点我收藏+]

1. 构建一个中间件,监测网站的响应状态,代码如下:

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 + "无匹配网址");

}

}

}

}

}

}

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

app.UseWatchNoFound();

这样该中间件就会响应短网址的六位字符串,如果匹配则重定向至对应的网址。

具体的短网址生成,我想就很简单,这里就不多说了,如果有需要,大家可以咨询我。

技术分享图片

平台界面

技术分享图片

输入网址

技术分享图片

自动生成当前域名的短网址

技术分享图片

后台管理

http://www.cnblogs.com/tianfengcc/p/7851931.html

开发短网址平台的思路

原文:https://www.cnblogs.com/chuancheng/p/8438583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!