MVC路由得到URL摆脱/索引 [英] MVC Routing get rid of /Index in URL

查看:116
本文介绍了MVC路由得到URL摆脱/索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时是这样产生的ActionLinks的动态列表:

I have a dynamic list of ActionLinks generated at run time like this:

@Html.ActionLink(x.Name, "Index", "User", new { x.ID }, null)

所以我打了用户控制器上的索引方法。

so I'm hitting the Index method on the User controller.

我也有我的RouteConfig.cs(这是一个MVC4应用程序)设置如下:

I also have my RouteConfig.cs (it's an MVC4 app) set as follows:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

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

这给了我的链接服务器/应用/用户/指数/ 1 在我的链接列表。

This gives me the link server/application/User/Index/1 in my list of links.

我需要什么列表中的产生和/或路由文件做更改为服务器/应用/用户/ 1

What do I need to do in the list generation and / or routing file to change this to server/application/User/1?

推荐答案

添加不需要的行动,但不会与其它路由冲突的路线

Add a route that doesn't need an action but won't clash with other routes

routes.MapRoute("User", "User/{id}", 
                         new { controller = "User", action = "Index" });

这篇关于MVC路由得到URL摆脱/索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆