如何在MVC中创建用户友好的URL?

时间:2015-02-13 09:15:58

标签: .net asp.net-mvc asp.net-mvc-4

我正在研究一个MVC项目。我的要求是我需要在浏览器中显示用户友好的URL。

这是我的mvc cshtml代码

@using (Html.BeginForm("AppointmentType", "Appointment", FormMethod.Post, new { enctype = "multipart/form-data", @id = "formAppointmentType", @class = "", ignoreThisBit = "Test-Appointment" }))
{
    <div class="container">
</div>
}

我的routeconfig文件代码

 routes.MapRoute("Resource", "{controller}/{action}/{id}/{ignoreThisBit}", new { controller = "Appointment", action = "Index", id = UrlParameter.Optional,
                                                                                            ignoreThisBit = UrlParameter.Optional
            }
            );

我也在webapiconfig中做了更改

    config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}/{ignoreThisBit}", new { id = RouteParameter.Optional, ignoreThisBit = RouteParameter.Optional }

在两个地方尝试后,我看到我的网址只显示http://localhost:8080/AppointmentType

我想展示Test-Appointment。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

如果您需要http://localhost:8080/AppointmentType/Test-Appointment,请在您的routeconfig文件代码中尝试不使用{id}:

routes.MapRoute("Resource", "{controller}/{action}/{ignoreThisBit}", new { controller = "Appointment", action = "Index", ignoreThisBit = UrlParameter.Optional });

答案 1 :(得分:0)

您只需要使用显式网址/AppointmentType

创建路线
routes.MapRoute(name: "Resource", 
    url: "AppointmentType", 
    defaults: new { controller = "Appointment", action = "AppointmentType" }

并将放在默认路线之前。

答案 2 :(得分:0)

你可以安装nuget包 - &#34;小写虚线路线&#34;来自NuGet Package Manager。要安装小写虚线路由,请在程序包管理器控制台中运行以下命令:PM> Install-Package LowercaseDashedRoute

更多细节&amp;示例源代码:https://www.nuget.org/packages/LowercaseDashedRoute/

https://github.com/AtaS/lowercase-dashed-route