【问题标题】:MVC 5 Url rewrite exact match onlyMVC 5 Url 仅重写完全匹配
【发布时间】:2017-07-09 12:55:00
【问题描述】:

我的网站上有一个不再存在 /app 的虚拟目录

我想将所有请求重定向到这个主页,但前提是它完全是 url /app 我尝试了以下,但这也重定向了 url /appointments

<rule name="RedirectToRoot" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="/app$" />
    <action type="Redirect" url="/" redirectType="Permanent"/>
</rule>

我知道问题出在我正在使用的正则表达式上,但我不确定如何解决它

【问题讨论】:

    标签: c# asp.net-mvc url-rewriting


    【解决方案1】:

    用这个解决了

    <rule name="RedirectToRoot" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="^app/(.*)" />
        <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
    </rule>
    

    【讨论】:

      【解决方案2】:

      您可以使用 MVC 5 中的以下解决方案。

      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
              // Add the below 
                          routes.MapMvcAttributeRoutes();
      // Add the below ^^
      
             routes.MapRoute(
                        name: "Default",
                        url: "{controller}/{action}/{id}",
                        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                    ); 
      

      RouteConfig.cs 你必须添加 routes.MapMvcAttributeRoutes();。完成后,您只需在操作方法上方标记 [Route("MyDenfinedURL")]

      [Route("MyDenfinedURL")]
       public ActionResult MyAction()
              {
                 //My do over the method    
                  return View();
              }
      

      一旦完成,您的工作就结束了。我的来源链接:http://www.dotnet-stuff.com/tutorials/aspnet-mvc/understanding-url-rewriting-and-url-attribute-routing-in-asp-net-mvc-mvc5-with-examples

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 1970-01-01
        • 2013-05-31
        相关资源
        最近更新 更多