【问题标题】:MVC4 URL rewrite aliasing for "prettier URLs"“更漂亮的 URL”的 MVC4 URL 重写别名
【发布时间】:2013-02-09 00:36:49
【问题描述】:

我目前对 Microsoft 的 MVC4 有点陌生,我不太了解路由。

我想要做的是让我的网址更易于阅读。目前我的网址如下所示:

  • foo.com/UserProfile/Details/6
  • foo.com/UserExperience/Details/
  • foo.com/UserSpecificController/Edit/8

所有用户控制器都以“用户”为前缀我想知道是否可以更改这些 URL,使它们看起来像这样:

  • foo.com/u/Profile/Details/6
  • foo.com/u/Experience/Details/
  • foo.com/u/SpecificController/Edit/8

我的第一次尝试是使用 IIS:

<rewrite>
        <rules>
            <rule name="AddTrailingSlashRule1" stopProcessing="true">
                <match url="(.*[^/])$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Redirect" url="{R:1}/" />
            </rule>
            <rule name="Pretty User Redirect" enabled="false" stopProcessing="true">
                <match url="User(.*)/(.*)" />
                <action type="Redirect" url="u/{R:1}/{R:2}" />
                <conditions>
                </conditions>
            </rule>
            <rule name="User pretty URL Rewrite">
                <match url="u/(.*)/(.*)" />
                <action type="Rewrite" url="User{R:1}/{R:2}" />
            </rule>
        </rules>
    </rewrite>

这工作得很好,除了我会在所有链接上得到/u/,无处不在......

例如: foo.com/Home/WhatWeDo/

会像这样: foo.com/u/Home/WhatWeDo/

这将是 404。

我使用的是默认路由配置

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

而且我所有的链接都是用@Html.ActionLink(...) 绘制的

如果有人能对此有所了解,将不胜感激。

【问题讨论】:

    标签: iis razor asp.net-mvc-4 asp.net-mvc-routing url-rewrite-module


    【解决方案1】:

    删除您的自定义 IIS 重写规则并将其插入到您的默认路由之前

    routes.MapRoute(
        name: "UserProfile",
        url: "u/Profile/{action}/{id}",
        defaults: new { controller = "Home", id = UrlParameter.Optional }
    );
    
    routes.MapRoute(
       name: "UserExperience",
       url: "u/Experience/{action}/{id}",
       defaults: new { controller = "Home", id = UrlParameter.Optional }
    );
    

    编辑:区域

    您可以使用区域对相关控制器进行分组

    Profile controller within a User area
    /User/Profile/Details
    
    Experience controller
    /User/Experience/Details
    

    然后该区域的 RegisterArea 只需一个自定义规则来替换您的路线中的“u”

    context.MapRoute(
        "UsersRoute",
        "u/{controller}/{action}/{id}",
        new { Controller = "Home", action = "Index", id = UrlParameter.Optional },
        new string[] { "MyNamespace.MyProj.Areas.User.Controllers" }
    );
    

    【讨论】:

    • 这看起来有点优雅,我必须为我的每个控制器都这样做吗?有没有办法添加我自己的代码/模块来重写我想要的 URL?
    • 也许你想要项目中的区域。然后,您将更改控制器类名称,而不必担心自定义路由/重写。
    猜你喜欢
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多