简体   繁体   English

ASP.NET MVC 4自定义路由

[英]ASP.NET MVC 4 Custom Routing

I have an area for admin functionality. 我有一个用于管理员功能的区域。 I want my URLs to look like so: 我希望我的网址看起来像这样:

admin/{controller}/{slug}.{id}/{action} (ie admin/user/getoutofmylab.1/edit) admin/{controller}/{action} (ie admin/user, this is just the index page of the user controller) admin / {controller} / {slug}。{id} / {action} (即admin / user / getoutofmylab.1 / edit) admin / {controller} / {action} (即admin / user,这只是索引页用户控制器)

When I browse from the root of my app to the user controller, I get admin/user. 当我从应用程序的根目录浏览到用户控制器时,我得到了admin / user。 Links for the edit action of the user controller generate correctly. 用户控制器的编辑操作的链接会正确生成。 However, if I link to the index action from within any other action in that controller, the URL is generated as such: admin/user/{slug}.{id}, which, if clicked, generates a 404 not found error. 但是,如果我从该控制器中的任何其他操作中链接到索引操作,则将这样生成URL:admin / user / {slug}。{id},如果单击该URL,则会生成404 not found错误。

Here's my AdminAreaRegisration: 这是我的AdminAreaRegisration:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRouteLowercase(
            "Admin_Applications",
            "admin/{controller}/{slug}.{id}/{action}",
            new { controller = "Application", action = "Index", id = UrlParameter.Optional, slug = "" },
            new string[] { "Web.Portal.Areas.Admin.Controllers" }
            );

        context.MapRouteLowercase(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Web.Portal.Areas.Admin.Controllers" }
            );
    }

How can I achieve the desired result? 如何获得理想的结果?

Thanks 谢谢

似乎使用Html.RouteLink并指定操作有效,而不是使用Html.ActionLink:@ Html.RouteLink(“ List Applications”,“ Admin_Default”,new {action =“ Index”})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM