在 ProductController
上执行 Detail(int id)
操作,我可以通过
/Product/Detail/32
但如果我这样做
Product/Detail
我还访问了同一个 Controller ,但没有传递 id
。怎么才能让参数成为必填项,否则返回404(根本不执行controller action,比如不匹配路由)
public ActionResult Detail(int id) {
// some fancy code that get the product by id
return View()
}
路线:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Application", action = "Index", id = UrlParameter.Optional } defaults);
我知道如果(未找到产品)返回 HttpNotFound()
我可以做,这适用于大多数情况,但我想知道是否有一种方法可以让 Controller Action 在没有通过的情况下甚至无法达到争论
编辑:
/ <-- homepage
/Product/List <-- List of products
/Product/Detail <-- return 404
/Product/Detail/10 <-- Product Details id 10
现在,我想知道是否有任何方法可以支持这种“简单”的场景。 Controller 上的操作是:
ApplicationController{
public ActionResult Index() {}
}
ProductController {
public ActionResult List(){}
public ActionResult Detail(int id){}
}
当前路由只是默认的:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Application", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
添加建议的路由没有按预期工作,因为要么在 /Product/Detail
上返回 404
但也在 上返回
和 404
>/Product/List/
。
或(其他建议)
ActionResult Detail(int id)
被调用或者没有在请求中发送参数,这是这个问题的目的是知道是否有可能不匹配 url /Product/Detail
完全没有它的 id 参数。
最佳答案
改变路由
只需为它提供路由并删除 id
默认为可选参数:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index" } // defaults
);
但这些默认值永远不会被使用,所以:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}" // URL with parameters
);
这是需要定义的实际路线。
您的 controller
和 action
永远不会从 URL 中省略,因为 id
是必需的并且是 URL 中的最后一个定义,这意味着第一对也必须存在。
我不确定这是否正是您所需要的,但根据您问题的当前状态,这应该可以解决您的问题。但是如果你需要你的 id 有一些预定义的值,你可以在你的路由定义中给它一个不同的值:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = 1 } // defaults
);
这使得可以从 URL 中省略所有这三个,并且它们都将具有特定的值。
定义id
格式的路由约束
您还可以使用路由约束来告诉路由 URL 参数应该是什么样子。由于您的 id
似乎必须是数字,这也是一种可能性:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index" }, // defaults
new { id = "\d+" } // constraints
);
修改后
实际上有两种方法可以解决您的问题。
- 为某些路由定义所需的
id
的正确路由 - 操作方法选择器过滤器以声明方式标记它们需要特定参数的操作
方案一:路由
这个定义了几个路由定义,但是硬编码了需要 id
参数的操作:
routes.MapRoute(
"RequiresId",
"{controller}/{action}/{id}", // URL with parameters
null,
new { action = "Detail" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}" // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = "(?!Detail).+" } // any action except "Detail"
);
第一个路由定义所有具有操作方法Detail
的 Controller 都需要id
参数。这很简单,只要具有这些操作的所有 Controller 具有相同的要求(您的情况可能就是这样)。但如果不是这样,事情就会变得更加复杂,因为您必须为每个 Controller 提供约束。
方案二:Action Method Selector Filter
此解决方案仅需要带有可选 id
的默认路由。自定义操作方法选择器过滤器(鲜为人知且很少使用)将帮助您编写如下代码:
[RequiresRouteValues("id, name")]
public ActionResult Detail(int id, string name)
{
...
}
您可以将它放在那些需要它的方法上。如果该特定参数不存在, Controller 操作调用程序将无法找到合适的方法,因此返回 404。
我已经详细讨论了这个 on my blog .它还包括过滤器的代码,如下所示:
/// <summary>
/// Represents an attribute that is used to restrict action method selection based on route values.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class RequiresRouteValuesAttribute : ActionMethodSelectorAttribute
{
#region Properties
/// <summary>
/// Gets required route value names.
/// </summary>
public ReadOnlyCollection<string> Names { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether to include form fields in the check.
/// </summary>
/// <value><c>true</c> if form fields should be included; otherwise, <c>false</c>.</value>
public bool IncludeFormFields { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to include query variables in the check.
/// </summary>
/// <value>
/// <c>true</c> if query variables should be included; otherwise, <c>false</c>.
/// </value>
public bool IncludeQueryVariables { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RequiresRouteValuesAttribute"/> class.
/// </summary>
private RequiresRouteValuesAttribute()
{
this.IncludeFormFields = true;
this.IncludeQueryVariables = true;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequiresRouteValuesAttribute"/> class.
/// </summary>
/// <param name="commaSeparatedNames">Comma separated required route values names.</param>
public RequiresRouteValuesAttribute(string commaSeparatedNames)
: this((commaSeparatedNames ?? string.Empty).Split(','))
{
// does nothing
}
/// <summary>
/// Initializes a new instance of the <see cref="RequiresRouteValuesAttribute"/> class.
/// </summary>
/// <param name="names">Required route value names.</param>
public RequiresRouteValuesAttribute(IEnumerable<string> names)
: this()
{
if (names == null || names.Count().Equals(0))
{
throw new ArgumentNullException("names");
}
// store names
this.Names = new ReadOnlyCollection<string>(names.Select(val => val.Trim()).ToList());
}
#endregion
#region ActionMethodSelectorAttribute implementation
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>
/// true if the action method selection is valid for the specified controller context; otherwise, false.
/// </returns>
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
// always include route values
HashSet<string> uniques = new HashSet<string>(controllerContext.RouteData.Values.Keys);
// include form fields if required
if (this.IncludeFormFields)
{
uniques.UnionWith(controllerContext.HttpContext.Request.Form.AllKeys);
}
// include query string variables if required
if (this.IncludeQueryVariables)
{
uniques.UnionWith(controllerContext.HttpContext.Request.QueryString.AllKeys);
}
// determine whether all route values are present
return this.Names.All(val => uniques.Contains(val));
}
#endregion
}
第一个使具有多个 Controller 和与之相关的不同约束的应用程序变得复杂。第二种优雅,适用于简单和复杂的场景。
我当然会选择方案 2。但在这种情况下,请将我视为有偏见的开发人员。
关于asp.net-mvc - 有没有可能是MVC应用中controller参数需要,路由不匹配空参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13820866/