【问题标题】:Different domain and url shortener for asp.net mvc appasp.net mvc 应用程序的不同域和 url 缩短器
【发布时间】:2021-01-18 18:12:59
【问题描述】:

我想实现一个简单的 URL 缩短功能,比如 Bitly。

我的控制器名称:WebController

我的操作名称:重定向

顾名思义,该操作会将用户从短 URL 重定向到完整 URL。

要调用此操作,我需要:https://myappdomain.com/web/redirect?id=3422

但我希望能够以更短的方式使用不同(更短)的域调用此功能,而无需调用操作名称:https://shorterdomain.com/3422

你能指导我怎么做吗?我什至不知道要搜索什么:(

【问题讨论】:

    标签: asp.net asp.net-mvc model-view-controller


    【解决方案1】:

    添加到较短 URL 的路由,以便 MVC 知道哪个控制器和操作将处理请求。像这样的:

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(name: "redirection",
                    pattern: "{id:int}",
                    defaults: new { controller = "web", action = "redirect" });
    
        ... your existing routes
    });
    

    【讨论】:

    • 谢谢!我想为此使用不同的域(而不是应用程序中的域)这一事实怎么样?你能帮我吗?
    • 要使用不同的域,您可以在 IIS 中设置绑定,以便两个域都指向您的 Web 应用程序。这将允许人们使用任一域访问您的站点。要将路由限制到特定域,请在 MapControllerRoute() 调用之后添加 .RequireHost("shorterdomain.com");
    • 这里是一个链接,显示如何通过主机限制路由。 docs.microsoft.com/en-us/aspnet/core/fundamentals/…
    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 2020-05-05
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多