问题描述
我重写的问题,因为答案为止告诉我,我还没有定义它不够好。我会离开原来的问题下面的参考。
I'm rewriting the question, as the answers so far show me that I have not defined it good enough. I'll leave the original question for reference below.
当您设置路由,您可以指定不同的URL /路由部分违约。让我们考虑的例子,VS向导生成:
When you set up your routing you can specify defaults for different url/route parts. Let's consider example that VS wizard generates:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "DefaultPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
在本实施例中,如果未指定控制器,DefaultPageController将被使用,将被使用,如果不是指定的动作是索引的动作。
该网址通常如下:的http:// mysite的/ myController的/ MyAction
In this example if controller is not specified, DefaultPageController will be used and if action is not specified "Index" action will be used.The urls will generally look like: http://mysite/MyController/MyAction
.
如果没有在网址没有动作是这样的:的http:// mysite的/ myController的
则将使用索引操作
If there is no action in Url like this: http://mysite/MyController
then the index action will be used.
现在让我们假设我有我的控制器指数AnotherAction两个动作。这与它们对应的网址是的http:// mysite的/ myController的
和的http:// mysite的/ myController的/ AnotherAction
分别。我的索引行动接受一个参数,ID。所以,如果我需要一个参数传递给我的索引行动,我可以这样做:的http:// mysite的/ myController的/索引/ 123
。请注意,不像在网址的http:// mysite的/ myController的
,我必须明确地指定索引操作。我想要做的是能够通过的http:// mysite的/ myController的/ 123
而不是的http:// mysite的/ myController的/索引/ 123
。我不需要指数在这个网址我想MVC引擎承认,当我要求的http:// mysite的/ myController的/ 123
,即123不操作(因为我还没有定义与此名称的动作),而是一个参数,以我的默认操作索引。我如何设置路由实现这一目标?
Now let's assume I have two actions in my controller Index and AnotherAction. The urls that correspond to them are http://mysite/MyController
and http://mysite/MyController/AnotherAction
respectively. My "Index" action accepts a parameter, id. So If I need to pass a parameter to my Index action, I can do this: http://mysite/MyController/Index/123
. Note, that unlike in URL http://mysite/MyController
, I have to specify the Index action explicitly. What I want to do is to be able to pass http://mysite/MyController/123
instead of http://mysite/MyController/Index/123
. I do not need "Index" in this URL I want the mvc engine to recognize, that when I ask for http://mysite/MyController/123
, that 123 is not an action (because I have not defined an action with this name), but a parameter to my default action "Index". How do I set up routing to achieve this?
下面是问题的原来的措辞。
Below is the original wording of the question.
我有definded这样两种方法控制器
I have a controller with two methods definded like this
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(SomeFormData data)
{
return View();
}
这让我来处理URL像的http://网站/页
既当用户浏览到该URL(GET),当他们随后回发的形式( POST)。
This allows me to process Url like http://website/Page
both when the user navigates to this url (GET) and when they subsequently post back the form (POST).
现在,当我处理回发,在某些情况下,我想将浏览器重定向到这个网址:
Now, when I process the post back, in some cases I want to redirect the browser to this url:
http://website/Page/123
其中123是一些整数,我需要一种方法在我的控制器来处理这个网址。
Where 123 is some integer, and I need a method to process this url in my controller.
我如何设置路由,所以这个作品?目前,我有这样的向导生成的默认路由:
How do I set up the routing, so this works? Currently I have "default" routing generated by the wizard like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "DefaultPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
我尝试添加这样的另一个控制器方法:
I tried adding another controller method like this:
public ActionResult Index(int id)
{
return View();
}
但是,这并不工作,因为暧昧动作抛出异常:
But this doesn't work as ambiguous action exception is thrown:
行动索引当前请求
控制器类型的PageController
就是下面的暧昧
操作方法:
System.Web.Mvc.ActionResult指数()上
类型的PageController
System.Web.Mvc.ActionResult
在类型的PageController指数(Int32)已
我要补充一点,我也有此控制器中的其他操作。 会工作。
I must add, that I also have other actions in this controller. This would have worked if I didn't.
推荐答案
下面是这可怎么原来决心:
Here is how this can be resoled:
您可以创建一个路由约束,以指示路由引擎,如果你有网址,看起来像一个数字(123)的东西,那么这是默认操作的操作参数,如果你有一些不看起来像一个数字(AnotherAction),那么它的行动。
You can create a route constraint, to indicate to the routing engine, that if you have something in url that looks like a number (123) then this is an action parameter for the default action and if you have something that does not look like a number (AnotherAction) then it's an action.
考虑这code:
routes.MapRoute(
"MyController", "MyController/{productId}",
new {controller="My", action="Index"},
new {productId = @"\d+" });
这个路由定义将只在 HTTP匹配后myController的数值:// mysite的/ myController的/ 123
所以它不会与调用同一个控制器上的另一个动作干扰。
This route definition will only match numeric values after MyController in http://mysite/MyController/123
so it will not interfere with calling another action on the same controller.
来源:
这篇关于asp.net mvc的路由:如何使用默认的动作,但非默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!