简体   繁体   English

Area中控制器的URL太长,如何缩短URL?

[英]Controller's URL in Area is too long, how to shorten URL?

I am new to Area term in MVC and I want to use it. 我是MVC中Area术语的新手,我想使用它。 I have the following directories that points to the controller in Area. 我有以下目录指向Area中的控制器。

Areas > Admin > Controllers > AdminController

When I want to visit the Index Action of AdminController, I need to visit http://localhost/Admin/Admin . 当我想访问AdminController的Index Action时,我需要访问http:// localhost / Admin / Admin I want to get rid of the second "Admin". 我想摆脱第二个“管理员”。 I want to type http://localhost/Admin/ only. 我只想输入http:// localhost / Admin / How can I do that? 我怎样才能做到这一点?

AdminAreaRegistration.cs has the following MapRoute AdminAreaRegistration.cs具有以下MapRoute

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

You have not specified a default value for the controller so unless you include it in the url, the routing engine has no way it identify which controller you want to navigate to. 您尚未为控制器指定默认值,因此,除非您将其包含在url中,否则路由引擎无法确定您要导航至哪个控制器。 You can solve this by giving a default value for the controller name 您可以通过提供控制器名称的默认值来解决此问题

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);

While that means that ../Admin will navigate to the Index() method of AdminController , it still means that if you want to navigate to another method in AdminController , your url will need to be ../Admin/Admin/AnotherMethod which is probably not what you want. 虽然这意味着../Admin将导航到AdminControllerIndex()方法,但这仍然意味着,如果要导航到AdminController另一个方法,则您的网址将需要为../Admin/Admin/AnotherMethod ,即可能不是您想要的。

The purpose of areas is to logically group your controllers and methods. 区域的目的是对控制器和方法进行逻辑分组。 For example a shopping cart app might have a ProductsController where users might navigate to ../Product to display a list of products or ../Product/Details/1 to display details of a product. 例如,一个购物车的应用程序可能有一个ProductsController ,用户可以浏览到../Product显示的产品或列表../Product/Details/1显示产品的详细信息。 But the app might need other methods for suppliers to create and edit their products so you would create a separate Suppliers area where ../Suppliers/Products would navigate to their list of products, and ../Suppliers/Products/Edit/1 would allow them to update details of their product. 但是该应用可能需要供供应商使用其他方法来创建和编辑其产品,因此您将创建一个单独的“ Suppliers区域, ../Suppliers/Products Suppliers ../Suppliers/Products将导航至其产品列表, ../Suppliers/Products/Edit/1将允许他们更新其产品的详细信息。

Having a AdminController in a Admin area does not really make sense, and I suggest that it should be HomeController if it contains general methods associated with admin tasks (and the route definition would then be new { controller = "Home", .. } Admin区域中放置AdminController并没有多大意义,我建议如果它包含与管理任务相关联的常规方法,则应使用HomeController (然后路由定义将是new { controller = "Home", .. }

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

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