[英]How To Prevent Multiple ASP.NET MVC Route Mappings
I have an ASP.NET MVC routing question. 我有一个ASP.NET MVC路由问题。 First, let me explain my areas setup.
首先,让我解释一下我的区域设置。 It's quite simple.
这很简单。
Areas
|
+--Foo
|
+--Controllers
|
+--BarController.cs
I have a folder in my areas called "Foo" and controller called "BarController.cs" The Bar controller has several methods named "DoStuff1()", "DoStuff2()", etc. 我的区域中有一个名为“Foo”的文件夹和名为“BarController.cs”的控制器.Bar控制器有几个名为“DoStuff1()”,“DoStuff2()”等的方法。
My website uses the following URLs: 我的网站使用以下网址:
/foo/bar/15
/foo/bar/dostuff1
/foo/bar/dostuff2
The first URL requires an id and uses the default Index() method in the Bar controller to populate the webpage with a view and model. 第一个URL需要一个id,并使用Bar控制器中的默认Index()方法使用视图和模型填充网页。
In the second and third URLs, I'm using them for jQuery ajax calls. 在第二个和第三个URL中,我将它们用于jQuery ajax调用。
Here is the code from my area registrion 这是我所在区域注册的代码
context.MapRoute(null, "Foo/Bar/DoStuff1", new
{
action = "DoStuff1",
controller = "Bar"
});
context.MapRoute(null, "Foo/Bar/DoStuff2", new
{
action = "DoStuff2",
controller = "Bar"
});
context.MapRoute(null, "Foo/Bar/{id}", new
{
action = "Index",
controller = "Bar"
});
My problem is that for each new controller method I create, I have to add another route mapping in the area registrion file. 我的问题是,对于我创建的每个新控制器方法,我必须在区域registrion文件中添加另一个路由映射。 For example, if I add the method DoStuff3(), I'll need to add this to the area registration:
例如,如果我添加方法DoStuff3(),我需要将其添加到区域注册:
context.MapRoute(null, "Foo/Bar/DoStuff3", new
{
action = "DoStuff3",
controller = "Bar"
});
How can I create a generic route mapping to handle the URLs I mentioned above that doesn't require new additions to the area registration file for new controller methods? 如何创建通用路由映射来处理上面提到的URL,这些URL不需要为新控制器方法添加区域注册文件?
You can pull out the controller action. 你可以拉出控制器动作。
Write the URL like this: 写下这样的URL:
"Foo/Bar/{action}"
Additionally, you can pull out the controller as well, and write 此外,您也可以拔出控制器并写入
"Foo/{controller}/{action}"
In this case, action = "Index"
provides a default value of "Index" if no action parameter is provided. 在这种情况下,如果没有提供动作参数,
action = "Index"
提供默认值“Index”。
In this case, you need to disambiguate between "Foo/Bar/{action}"
and "Foo/Bar/{id}"
. 在这种情况下,您需要消除
"Foo/Bar/{action}"
和"Foo/Bar/{id}"
之间的歧义。 Since matching is done in order, you'll want to put the id
route first, and add a numeric constraint to the id
parameter. 由于匹配是按顺序完成的,因此您需要先将
id
路由放入,然后在id
参数中添加数字约束 。 This allows valid numeric ids to match it, and action names to skip down to the next route. 这允许有效的数字ID匹配它,并且动作名称可以跳到下一个路径。 Your two routes would look like this:
你的两条路线看起来像这样:
context.MapRoute(null, "Foo/Bar/{id}", new
{
action = "Index",
controller = "Bar"
},
new { id = @"\d+" });
context.MapRoute(null, "Foo/Bar/{action}", new
{
action = "Index", //optional default parameter, makes the route fall back to Index if no action is provided
controller = "Bar"
});
The default routing that comes with MVC templates are good for most of the needed route configurations. MVC模板附带的默认路由适用于大多数所需的路由配置。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.