ASP.NET MVC 路由,需要帮助创建路由

发布于 2024-09-14 23:16:39 字数 2615 浏览 9 评论 0原文

我当前的路由规则是

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
);

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

/Admin/Index

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

/News/Index

但我需要它像:

/Admin/News/Index

当前的设置有点有效,但我主页上的链接被错误的规则捕获,并且将被重定向到,例如/Admin/Article /1 当它们应该只是 /Article/1 时

我在 StackOverflow 中搜索了答案,并找到了一些接近的答案 - 但我仍然不太了解路由以利用他们的答案。如有任何帮助,我们将不胜感激。

编辑

以下是来自主页的链接集合,这些链接被路由规则错误地捕获

<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>

此外,我的管理控制器只有一个索引操作。我有所有其他“管理”页面的控制器,例如我的 NewsController 具有索引、创建、编辑、删除、列出所有新闻文章以及执行增删改查操作的操作。我的结构是否错误?

我的 AdminController.cs

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

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


    }

返回的 Admin/Index 包含一个带有 ActionLinks 的菜单,就像

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

我的 NewsController.cs 具有用于执行 CRUD 操作的操作一样。我希望网址是

Admin/News/Edit/5

而不是

News/Edit/5

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-09-21 23:16:39

翻转两者:

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

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

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

如果这不起作用,请将新闻菜单链接的代码添加到您的问题。

Flip the two:

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

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

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

If that doesn't work, add the code for the News menu links to your question.

唔猫 2024-09-21 23:16:39

映射是从上到下,第一个可以申请的路由将被使用。在您的情况下,您在默认下有管理路由。由于可以使用默认值,因此您应该将管理路由置于默认值之上。问题是所有人都会使用这条路线。您需要更具体一点,也许您应该删除 {controller} ,以便只有管理操作才会使用该路由。需要更多信息才能提供更好的建议。

The mapping is from top to bottom, the first route that can apply will be used. In your case you have Admin route under Default. Since Default can be used it will, therefore you should place Admin route above Default. The problem with that is that all will use that route. You need to be a bit more specific, perhaps you should remove {controller} so that only Admin actions will use that route. Need more info to better advise.

复古式 2024-09-21 23:16:39

我需要使用区域。

请参阅:http://elegantcode.com/2010/03 /13/asp-net-mvc-2-areas/

我创建了一个名为 Admin 的新区域,将 AdminController.cs 重命名为 MenuController.cs,并将所有必需的控制器和视图放置在 Admin 区域文件夹内。在管理区域文件夹内有一个名为 AdminAreaRegistration.cs 的文件,其中包含

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 }
        );
    }
}

我必须添加的控制器 =“Menu”,这样当您导航到 /Admin 时,默认情况下它将带您到 /Admin/Menu/Index 。现在,区域文件夹中的任何控制器都可以访问,例如 /Admin/News/Edit/5 等。感谢您的尝试,我应该更清楚地表明我是一个绝对的 MVC 菜鸟,并且一开始就不了解 Areas。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文