I have this set of routes:
我有一组路线:
routes.MapRoute(
"IssueType",
"issue/{type}",
new { controller = "Issue", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Here is the controller class:
这是控制器类:
public class IssueController : Controller
{
public ActionResult Index()
{
// todo: redirect to concrete type
return View();
}
public ActionResult Index(string type)
{
return View();
}
}
why, when i request http://host/issue i get The current request for action 'Index' on controller type 'IssueController' is ambiguous between the following action methods:
I expect that first one method should act when there is no parameters, and second one when some parameter specified.
为什么,当我请求http://host/issue时,在控制器类型为‘问题控制主’的操作'Index'的当前请求在以下操作方法中是不明确的:我希望第一个方法在没有参数时应该执行,第二个方法在指定参数时执行。
where did i made mistake?
我哪里做错了?
UPD: possible duplicate: Can you overload controller methods in ASP.NET MVC?
UPD:可能重复:可以在ASP中重载控制器方法吗?净MVC吗?
UPD 2: due to the link above - there is no any legal way to make action overloading, is it?
upd2:由于上面的链接-没有任何合法的方式使诉讼超载,是吗?
UPD 3: Action methods cannot be overloaded based on parameters (c) http://msdn.microsoft.com/en-us/library/system.web.mvc.controller%28VS.100%29.aspx
UPD 3:不能基于参数(c)来重载操作方法
10
I would have one Index method that looks for a valid type variable
我将有一个查找有效类型变量的索引方法。
public class IssueController : Controller
{
public ActionResult Index(string type)
{
if(string.isNullOrEmpty(type)){
return View("viewWithOutType");}
else{
return View("viewWithType");}
}
}
EDIT:
编辑:
How about creating a custom attribute that looks for a specific request value as in this post StackOverflow
如何创建一个自定义属性来查找特定的请求值,就像这个post StackOverflow那样
[RequireRequestValue("someInt")]
public ActionResult MyMethod(int someInt) { /* ... */ }
[RequireRequestValue("someString")]
public ActionResult MyMethod(string someString) { /* ... */ }
public class RequireRequestValueAttribute : ActionMethodSelectorAttribute {
public RequireRequestValueAttribute(string valueName) {
ValueName = valueName;
}
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
return (controllerContext.HttpContext.Request[ValueName] != null);
}
public string ValueName { get; private set; }
}
5
I ran into a similar situation where I wanted my "Index" action to handle the rendering if I had an ID specified or not. The solution I came upon was to make the ID parameter to the Index method optional. For example, I originally tried having both:
我遇到了类似的情况,我想要我的“索引”操作来处理呈现,如果我有一个ID指定或不指定。我遇到的解决方案是使索引方法的ID参数可选。例如,我最初尝试两者兼得:
public ViewResult Index()
{
//...
}
// AND
public ViewResult Index(int entryId)
{
//...
}
and I just combined them and changed it to:
我把它们组合起来,变成:
public ViewResult Index(int entryId = 0)
{
//...
}
1
You can do it using an ActionFilterAttribute that checks the parameters using reflection (I tried it) but it's a bad idea. Each distinct action should have its own name.
您可以使用ActionFilterAttribute使用反射(我尝试过)检查参数,但这不是一个好主意。每个不同的行为都应该有自己的名字。
Why not just call your two methods "Index" and "Single", say, and live with the limitation on naming?
为什么不把你的两种方法叫做“索引”和“单”,并且在命名上有限制呢?
Unlike methods that are bound at compile time based on matching signatures, a missing route value at the end is treated like a null.
与基于匹配签名在编译时绑定的方法不同,末尾缺失的路由值被视为null。
If you want the [hack] ActionFilterAttribute that matches parameters let me know and I'll post a link to it, but like I said, it's a bad idea.
如果您想要匹配参数的[hack] ActionFilterAttribute,请让我知道,我将发布到它的链接,但是就像我说的,这不是一个好主意。
1
All you have to do is mark your second Action with [HttpPost]. For instance:
你所要做的就是用[HttpPost]标记你的第二个动作。例如:
public class IssueController : Controller
{
public ActionResult Index()
{
// todo: redirect to concrete type
return View();
}
[HttpPost]
public ActionResult Index(string type)
{
return View();
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/04/12/37fde9102ebdf73e4fce4fb694e0dcc9.html。