Asp.net mvc 当查询字符串参数不正确时,如何在ASP.NETMVC中返回404页
让我们假设我有以下动作Asp.net mvc 当查询字符串参数不正确时,如何在ASP.NETMVC中返回404页,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,让我们假设我有以下动作 public ViewResult Products(string color) {...} 以及将url“产品”映射到此操作的路径 根据SEO提示链接/products?color=red应该返回 200行 但是link/products?someOtherParametr=someValue 404找不到 因此,问题是-如何处理不存在的查询参数并返回404在这种情况下考虑到接受的答案,有一个特殊的操作结果可以满足您的期望 public class HomeContr
public ViewResult Products(string color)
{...}
以及将url“产品”映射到此操作的路径
根据SEO提示链接/products?color=red
应该返回
200行
但是link/products?someOtherParametr=someValue
404找不到
因此,问题是-如何处理不存在的查询参数并返回404在这种情况下考虑到接受的答案,有一个特殊的
操作结果
可以满足您的期望
public class HomeController : Controller
{
public ViewResult Products(string color)
{
if (color != "something") // it can be replaced with any guarded clause(s)
return new HttpNotFoundResult("The color does not exist.");
...
}
}
更新:
public class HomeController : Controller
{
public ViewResult Products(string color)
{
if (Request.QueryString.Count != 1 ||
Request.QueryString.GetKey(0) != "color")
return new HttpNotFoundResult("the error message");
...
}
}
考虑到目前的公认答案,有一个特殊的
ActionResult
可以满足您的期望
public class HomeController : Controller
{
public ViewResult Products(string color)
{
if (color != "something") // it can be replaced with any guarded clause(s)
return new HttpNotFoundResult("The color does not exist.");
...
}
}
更新:
public class HomeController : Controller
{
public ViewResult Products(string color)
{
if (Request.QueryString.Count != 1 ||
Request.QueryString.GetKey(0) != "color")
return new HttpNotFoundResult("the error message");
...
}
}
在执行操作方法之前验证此操作。此方法适用于您项目的所有控制者或特定操作方法。
控制器动作方法
[attr] // Before executing any Action Method, Action Filter will execute to
//check for Valid Query Strings.
public class ActionResultTypesController : Controller
{
[HttpGet]
public ActionResult Index(int Param = 0)
{
return View();
}
[HttpPost]
public ActionResult Index(MyViewModel obj)
{
return View(obj);
}
}
动作过滤器
public class attr : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters.Count == 0 &&
System.Web.HttpContext.Current.Request.QueryString.Count > 0)
{
//When no Action Parameter exists and Query String exists.
}
else
{
// Check the Query String Key name and compare it with the Action
// Parameter name
foreach (var item in System.Web.HttpContext
.Current
.Request.QueryString.Keys)
{
if (!filterContext.ActionParameters.Keys.Contains(item))
{
// When the Query String is not matching with the Action
// Parameter
}
}
}
base.OnActionExecuting(filterContext);
}
}
如果您注意上面的代码,我们将检查下面屏幕显示的操作参数
或
我们可以这样做。下面提到的代码将写入OnActionExecuting
方法覆盖中
filterContext.Result = new HttpStatusCodeResult(404);
在执行操作方法之前验证此操作。此方法适用于您项目的所有控制者或特定操作方法。
控制器动作方法
[attr] // Before executing any Action Method, Action Filter will execute to
//check for Valid Query Strings.
public class ActionResultTypesController : Controller
{
[HttpGet]
public ActionResult Index(int Param = 0)
{
return View();
}
[HttpPost]
public ActionResult Index(MyViewModel obj)
{
return View(obj);
}
}
动作过滤器
public class attr : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters.Count == 0 &&
System.Web.HttpContext.Current.Request.QueryString.Count > 0)
{
//When no Action Parameter exists and Query String exists.
}
else
{
// Check the Query String Key name and compare it with the Action
// Parameter name
foreach (var item in System.Web.HttpContext
.Current
.Request.QueryString.Keys)
{
if (!filterContext.ActionParameters.Keys.Contains(item))
{
// When the Query String is not matching with the Action
// Parameter
}
}
}
base.OnActionExecuting(filterContext);
}
}
如果您注意上面的代码,我们将检查下面屏幕显示的操作参数
或
我们可以这样做。下面提到的代码将写入OnActionExecuting
方法覆盖中
filterContext.Result = new HttpStatusCodeResult(404);
受保护的覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
var actionParameters=filterContext.actionParameters.Keys;
var queryParameters=filterContext
.RequestContext
.HttpContext
要求
.QueryString先生
.钥匙;
//如果我们将任何查询字符串参数传递给操作,该参数
//存在于操作中我们应该返回404状态代码
if(queryParameters.Cast().Any)(queryParameter
=>!actionParameters.Contains(queryParameter)))
filterContext.Result=新的HttpStatusCodeResult(404);
}
实际上,如果您不想在每个控制器中都写入此命令,则应重写DefaultControllerFactory受保护的重写void OnActionExecuting(ActionExecutingContext filterContext)
{
var actionParameters=filterContext.actionParameters.Keys;
var queryParameters=filterContext
.RequestContext
.HttpContext
要求
.QueryString先生
.钥匙;
//如果我们将任何查询字符串参数传递给操作,该参数
//存在于操作中我们应该返回404状态代码
if(queryParameters.Cast().Any)(queryParameter
=>!actionParameters.Contains(queryParameter)))
filterContext.Result=新的HttpStatusCodeResult(404);
}
事实上,如果你不想在每个控制器中都写这个,你应该重写DefaultControllerFactory我写的关于url/products?SomeOtherParameter=someValue而不是关于/products?color=SomeThing我在回答中写道,“它可以替换为任何受保护的子句”;所以,您可以通过检查QueryString对象来检查是否提供了适当的参数。这很简单。你想让我更新答案吗?是的,请。非常感谢itThanks,但我发现了更灵活的方式我一直在寻找这样简单的东西很长一段时间-从来都不知道这个存在。谢谢。我写了关于url/products?SomeOtherParameter=someValue而不是关于/products?color=somethingI在回答中写道,“它可以被任何保护条款替换”;所以,您可以通过检查QueryString对象来检查是否提供了适当的参数。这很简单。你想让我更新答案吗?是的,请。非常感谢itThanks,但我发现了更灵活的方式我一直在寻找这样简单的东西很长一段时间-从来都不知道这个存在。谢谢。很好,但是当我们有复杂的对象在运行时我们会遇到麻烦。参数扫描你能举个例子吗?很好,但是当我们有复杂的对象在运行时我们会遇到麻烦。参数扫描你能举个例子吗?