【问题标题】:ASP.NET MVC block a specific URL from the Default routeASP.NET MVC 阻止来自默认路由的特定 URL
【发布时间】:2018-08-23 07:26:32
【问题描述】:

在 ASP.NET MVC 中路由配置如下:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

我在 Controller 中使用[Route] 属性:

[Route("login")]
public ActionResult Login()
{
    return View();
}

我可以同时使用/loginAuth/Login 访问该页面。第二个链接显然来自默认路由,但我不希望该 URL 转到我的登录页面。

我该怎么做?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 routing asp.net-mvc-routing


    【解决方案1】:

    要阻止一条路线被服务,你可以使用IgnoreRoute,只要你把它放在你想要阻止的路线之前:

    routes.IgnoreRoute(url: "Auth/Login");
    
    // Register any custom routes here...
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2015-02-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多