While ASP.NET MVC will allow you to have two actions with the same name, . NET won't allow you to have two methods with the same signature - i.e. the same name and parameters. You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.
A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
ViewBag uses the dynamic feature that was introduced in to C# 4.0. It allows an object to have properties dynamically added to it. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class. ViewBag only transfers data from controller to view, not visa-versa.
MVC doesn't support method overloading based solely on signature, so this will fail:
public ActionResult MyMethod(int someInt) { /* ... */ }
public ActionResult MyMethod(string someString) { /* ... */ }
However, it does support method overloading based on attribute:
[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; }
}
In the above example, the attribute simply says "this method matches if the key xxx was present in the request." You can also filter by information contained within the route (controllerContext.RequestContext) if that better suits your purposes.
The parameters in your routes {roleId}
, {applicationName}
and {roleName}
don't match the parameter names in your action methods. I don't know if that matters, but it makes it tougher to figure out what your intention is.
Do your itemId's conform to a pattern that could be matched via regex? If so, then you can add a restraint to your route so that only url's that match the pattern are identified as containing an itemId.
If your itemId only contained digits, then this would work:
routes.MapRoute("AssignRemove",
"Items/{action}/{itemId}",
new { controller = "Items" },
new { itemId = "\d+" }
);
Edit: You could also add a constraint to the AssignRemovePretty
route so that both {parentName}
and {itemName}
are required.
Edit 2: Also, since your first action is just redirecting to your 2nd action, you could remove some ambiguity by renaming the first one.
// Method #1
public ActionResult AssignRemovePretty(string parentName, string itemName) {
// Logic to retrieve item's ID here...
string itemId = ...;
return RedirectToAction("Assign", itemId);
}
// Method #2
public ActionResult Assign(string itemId, string searchTerm, int? page) { ... }
Then specify the Action names in your routes to force the proper method to be called:
routes.MapRoute("AssignRemove",
"Items/Assign/{itemId}",
new { controller = "Items", action = "Assign" },
new { itemId = "\d+" }
);
routes.MapRoute("AssignRemovePretty",
"Items/Assign/{parentName}/{itemName}",
new { controller = "Items", action = "AssignRemovePretty" },
new { parentName = "\w+", itemName = "\w+" }
);
Another approach is to rename one of the methods so there is no conflict. For example
// GET: /Movies/Delete/5
public ActionResult Delete(int id = 0)
// POST: /Movies/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id = 0)
See http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part9-cs
Recently I took the chance to improve @Levi's answer to support a wider range of scenarios I had to deal with, such as: multiple parameter support, match any of them (instead of them all) and even match none of them.
Here's the attribute I'm using now:
/// <summary>
/// Flags an Action Method valid for any incoming request only if all, any or none of the given HTTP parameter(s) are set,
/// enabling the use of multiple Action Methods with the same name (and different signatures) within the same MVC Controller.
/// </summary>
public class RequireParameterAttribute : ActionMethodSelectorAttribute
{
public RequireParameterAttribute(string parameterName) : this(new[] { parameterName })
{
}
public RequireParameterAttribute(params string[] parameterNames)
{
IncludeGET = true;
IncludePOST = true;
IncludeCookies = false;
Mode = MatchMode.All;
}
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
switch (Mode)
{
case MatchMode.All:
default:
return (
(IncludeGET && ParameterNames.All(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
|| (IncludePOST && ParameterNames.All(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
|| (IncludeCookies && ParameterNames.All(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
);
case MatchMode.Any:
return (
(IncludeGET && ParameterNames.Any(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
|| (IncludePOST && ParameterNames.Any(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
|| (IncludeCookies && ParameterNames.Any(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
);
case MatchMode.None:
return (
(!IncludeGET || !ParameterNames.Any(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
&& (!IncludePOST || !ParameterNames.Any(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
&& (!IncludeCookies || !ParameterNames.Any(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
);
}
}
public string[] ParameterNames { get; private set; }
/// <summary>
/// Set it to TRUE to include GET (QueryStirng) parameters, FALSE to exclude them:
/// default is TRUE.
/// </summary>
public bool IncludeGET { get; set; }
/// <summary>
/// Set it to TRUE to include POST (Form) parameters, FALSE to exclude them:
/// default is TRUE.
/// </summary>
public bool IncludePOST { get; set; }
/// <summary>
/// Set it to TRUE to include parameters from Cookies, FALSE to exclude them:
/// default is FALSE.
/// </summary>
public bool IncludeCookies { get; set; }
/// <summary>
/// Use MatchMode.All to invalidate the method unless all the given parameters are set (default).
/// Use MatchMode.Any to invalidate the method unless any of the given parameters is set.
/// Use MatchMode.None to invalidate the method unless none of the given parameters is set.
/// </summary>
public MatchMode Mode { get; set; }
public enum MatchMode : int
{
All,
Any,
None
}
}
For further info and how-to implementation samples check out this blog post that I wrote on this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With