ASP.NET MVC区域:如何隐藏“区域"网址中的名称? [英] ASP.NET MVC Areas: How to hide "Area" name in URL?

查看:132
本文介绍了ASP.NET MVC区域:如何隐藏“区域"网址中的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行 MVC 2区域示例,其URL如下所示:

When running the MVC 2 Areas example that has a Blog Area and Blog Controller the URL looks like this:

http://localhost:50526/Blog/Blog/ShowRecent ,格式为:

RootUrl/AreaName/ControllerName/ActionName

RootUrl / AreaName / ControllerName / ActionName

刚刚发现了MVC区域,这似乎是组织代码的好方法,即为每个部分创建一个区域,在我的情况下,每个部分都有自己的控制器.这意味着每个AreaName = ControllerName.这样做的结果是Url中的双重AreaName/ControllerName路径,例如上面的/Blog/Blog/

Having just discovered MVC Areas, it seem like a great way to organise code, ie create an Area for each section, which in my case each section has its own controller. This means that each AreaName = ControllerName. The effect of this is the double AreaName/ControllerName path in the Url eg /Blog/Blog/ above

对路由没有完全清楚的了解,如何设置路由以不显示AreaName?

Not having a complete clear understanding of routing, how could I setup routing to not show the AreaName?

我正在尝试减少使用路由的工作量,因为它们似乎相互影响(即需要特定的排序),并且可能会引起严重的麻烦:-)在将现有的Webform应用程序转换为MVC时,我已经转换了一些核心部分,每个部分都有一个Controller和相当数量的View/Action,尽管大多数数据访问是在汇编中的代码,但是Model/ViewData类的数量却在增加……我目前正在Root中创建子文件夹这些部分(或区域)的Models/Views文件夹,希望创建区域将以相同的方式工作,除了组织代码(使用覆盖该区域的基本路线) 对此有何评论?

I am trying to reduce the amount of work with routes as these appear to effect each other (ie require specific ordering) and may cause major headaches :-) In converting an existing webform app to MVC, I have converted a couple of core sections, These have one Controller each and a fair amount of View/Actions and although most of the Data Access is code is in assemblies the number of Model/ViewData classes is growing... I am currently creating sub-folders in the Root Models/Views folders for these sections (or Areas) and was hoping that creating Areas would work the same way except having the code organised (with the use of a basic route that covers the Area) Any comment on this?

推荐答案

在每个区域的文件夹中,您将看到一个*AreaName*AreaRegistration.cs文件.这是区域路由规则的存储位置.默认情况下,生成它们时,它们将包含区域名称,然后再包含所有其他区域.问题是:如果从路由中删除区域名称文件夹",则路由将捕获所有标准" {controller}/{操作}/{id}请求.显然这不是您想要的..

Inside each area's folder you'll see a *AreaName*AreaRegistration.cs file. This is where the area routeing rules are stored. By default, as they are generated, they will contain the area name ahead of everything else.. The problem is: if you remove the area name "folder" from the route, the route will catch all "standard" {controller}/{action}/{id} requests. Which is obviously not what you want..

要解决此问题,您可以基于该路由中存在的控制器名称,在该路由上添加正则表达式过滤器.缺点呢?您将无法在应用程序中拥有两个具有相同名称的控制器(至少不使用标准路由.您始终可以想到使用不同的路由来访问它们:))

To overcome this you can add the regex filters on the routes, based on the controller names present in that route. The drawback? You won't be able to have two controllers with the same name within the app (at least not using the standard route.. You can always think of a different route to access them :) )

最后..具有这种结构:

In the end.. Having this structure:

/区域
/Areas/Blog/Controllers/BlogController.cs
/Areas/Blog/Controllers/FeedController.cs
/Areas/User/Controllers/UserController.cs
/Controllers/PageController.cs

/Areas
/Areas/Blog/Controllers/BlogController.cs
/Areas/Blog/Controllers/FeedController.cs
/Areas/User/Controllers/UserController.cs
/Controllers/PageController.cs

您应该拥有的是这样的: 在BlogAreaRegistration.cs中:

What you should have is sth like this: In BlogAreaRegistration.cs:

context.MapRoute(
    "Blog_default",
    "{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new { controller = "(Blog|Feed)" }
);

在UserAreaRegistration.cs中:

In UserAreaRegistration.cs:

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

在Global.asax.cs中:

In Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    context.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
}

请注意,在global.asax区域中注册是第一位的! :)

Note that in global.asax area registrations come first! :)

UPD: 根据您的问题更新: 在这里,如果要使用区域,则必须考虑到一件重要的事情:如果您具有区域间链接,则还必须在链接中提供区域名称.例如

UPD: Based on your question update: There us one major thing that you'll have to take into consideration if you will use areas: If you have a inter-area link, you'll also have to provide the area name in the link. E.g.

<%: Html.ActionLink("Link text", "Action", "Controller", new { area = "Blog", id = 4, title = "page-title" }); %>

您明白了.

关于多个模型/视图,此刻我正在遵循这样的结构

Regarding the multiple models/views, at the moment I'm following a structure like this

/Code///帮助程序,未移至库的扩展类
/Models/Data///EF类+验证类在这里
/Models/ViewModels/{controller}///查看每个控制器存储的模型

/Code/ // helper, extension classes that aren't moved to libraries
/Models/Data/ // The EF classes + validation classes are here
/Models/ViewModels/{controller}/ // view models stored per controller

到目前为止,它运行良好,并且我设法使解决方案保持相对有条理.正如我所说,到目前为止,我创建的唯一区域是Admin区域,因为它与网站的其余部分有很大不同:)

So far it works fine, and I managed to keep the solution relatively organised. As I stated, the only area that I created so far is the Admin area because it's that much different from the rest of the website :)

这篇关于ASP.NET MVC区域:如何隐藏“区域"网址中的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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