ASP.NET MVC路由,需要帮助建立路线 [英] ASP.NET MVC Routing, Need help creating route

查看:139
本文介绍了ASP.NET MVC路由,需要帮助建立路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的路由规则

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);routes.MapRoute(
    默认,//路线名称
    {控制器} / {行动} / {ID},// URL带参数
    新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
);routes.MapRoute(
    管理,//路线名称
    管理/ {控制器} / {行动} / {ID},// URL带参数
    新{控制器=管理,行动=索引,ID = UrlParameter.Optional} //参数默认
);

我有只能由管理员访问几个控制器。目前,我有一个菜单:

  /管理/索引

其中列出了一堆的操作链接。现在,点击任何这些链接重定向这样的例子:

  /新闻/索引

但我需要它像:

  /管理/新闻/索引

这个当前设置的排序作品,但链接在我的主页正在被错抓的规则和打算,例如/管理/条/ 1时,他们应该只是/条/ 1

我搜索StackOverflow的答案,并发现了一些接近 - 但我还是不明白路由不够好,利用他们的答案。任何援助AP preciated。

修改

下面是正在由路由规则捕获的从主页链接的集合不正确

 < D​​IV ID =MenuContainer中>                < UL ID =菜单>
                    <立GT;<%:Html.ActionLink(,关于,家,空,新的{ID =AboutMHN})%GT;< /李>
                    <立GT;<%:Html.ActionLink(,产品,家,空,新的{ID =产品})%GT;< /李>
                    <立GT;<%:Html.ActionLink(,HubLocator,家,空,新的{ID =HubLocator})%GT;< /李>
                    <立GT;<%:Html.ActionLink(,ResellerProgram,家,空,新的{ID =ResellerProgram})%GT;< /李>
                    <立GT;<%:Html.ActionLink(,ResellerLogin,家,空,新的{ID =ResellerLogin})%GT;< /李>
                    <立GT;<%:Html.ActionLink(,联系我们,家,空,新的{ID =联系我们})%GT;< /李>
                < / UL>            < / DIV>

另外,我的管理控制器只是有一个索引操作。我有控制器所有其他管理页面,例如我的NewsController有指标,创建,编辑,删除,上市所有的新闻文章,并执行CRUD操作的动作。难道我不当这个构造?

我AdminController.cs

  [授权(角色=管理员)]
    公共类AdminController:控制器
    {
        //
        // GET:/管理/        公众的ActionResult指数()
        {
            返回查看();
        }
    }

管理员/指数这将返回包含ActionLinks菜单就像

 <李><%:Html.ActionLink(新闻文章,索引,新闻报)%>< /李>

我NewsController.cs具有进行CRUD操作动作。我想的网址是

 管理​​/新闻/编辑/ 5

而不是

 新闻/编辑/ 5


解决方案

我需要使用的地方。

请参阅:的http://优雅code.com / 2010/03/13 / ASP净MVC-2区/

我创建了一个名为管理新的领域,我改名为AdminController.cs和MenuController.cs放置所有必需的控制器和视图的管理区域文件夹内。内部管理方面的文件夹有一个叫AdminAreaRegistration.cs包含文件

 公共类AdminAreaRegistration:AreaRegistration
{
    公众覆盖字符串AREANAME
    {
        得到
        {
            返回管理;
        }
    }    公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
    {
        context.MapRoute(
            Admin_default
            管理/ {控制器} / {行动} / {ID}
            新{控制器=菜单,行动=索引,ID = UrlParameter.Optional}
        );
    }
}

我不得不在控制器=菜单添加这样,当你浏览/管理它会带你在默认情况下/管理/菜单/索引。现在,任我在该地区的文件夹中的控制器都喜欢/管理/新闻/编辑/ 5等进行访问。感谢您的努力,我本来应该更清楚,我是一个绝对的MVC菜鸟,不知道的领域开始。

My current Routing rules are

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

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Admin", // Route name
    "Admin/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

I have several controllers that can only be accessed by the Admin. Currently I have a menu at:

/Admin/Index

Which lists a bunch of action links. Right now clicking any of those links redirects like this example:

/News/Index

But I need it to be like:

/Admin/News/Index

This current setup sort of works, but links on my homepage are being caught by the wrong rule and are going to, for example /Admin/Article/1 when they should be just /Article/1

I've searched StackOverflow for the answer, and found some that come close - but I still don't understand routing well enough to make use of their answers. Any assistance is appreciated.

EDIT

Here are a collection of links from the homepage that are being caught by the routing rules incorrectly

<div id="menucontainer">

                <ul id="menu">              
                    <li><%: Html.ActionLink(" ", "About", "Home", null, new { ID = "AboutMHN" })%></li>
                    <li><%: Html.ActionLink(" ", "Products", "Home", null,  new { ID = "Products" })%></li>
                    <li><%: Html.ActionLink(" ", "HubLocator", "Home", null, new { ID = "HubLocator" })%></li>
                    <li><%: Html.ActionLink(" ", "ResellerProgram", "Home", null, new { ID = "ResellerProgram" })%></li>
                    <li><%: Html.ActionLink(" ", "ResellerLogin", "Home",  null, new { ID = "ResellerLogin" })%></li>
                    <li><%: Html.ActionLink(" ", "ContactUs", "Home", null, new { ID = "ContactUs" })%></li>
                </ul>                 

            </div>

Also, my Admin controller just has an index action. I have controllers for all of the other 'Admin' pages, for example my NewsController has actions for index, create, edit, delete, for listing all the news articles, and performing crud operations. Am I structuring this incorrectly?

My AdminController.cs

 [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }


    }

The Admin/Index this returns contains a menu with ActionLinks just like

<li><%: Html.ActionLink("News Articles", "Index", "News") %></li>

My NewsController.cs has Actions for performing CRUD operations. I would like the url to be

Admin/News/Edit/5

Instead of

News/Edit/5

解决方案

I needed to use Areas.

See: http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/

I created a new Area called Admin, renamed my AdminController.cs to MenuController.cs and placed all of the required controllers and views inside of the Admin area folder. Inside the Admin area folder there is a file called AdminAreaRegistration.cs which contains

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller= "Menu", action = "Index", id = UrlParameter.Optional }
        );
    }
}

I had to add in the controller = "Menu" so that when you navigate to /Admin it will take you to /Admin/Menu/Index by default. Now any of my controllers in the area folder are accessible like /Admin/News/Edit/5 and so forth. Thanks for your attempts, I should have been more clear that I am an absolute MVC noob and didn't know about Areas to begin with.

这篇关于ASP.NET MVC路由,需要帮助建立路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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