C# 如何在ASP.NETMVC3中重定向到ActionResult中的路由
我正在尝试创建一个访问被拒绝的ActionResult以从我的控制器返回。我有以下实现C# 如何在ASP.NETMVC3中重定向到ActionResult中的路由,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我正在尝试创建一个访问被拒绝的ActionResult以从我的控制器返回。我有以下实现 public class AccessDeniedResult : ActionResult { public override void ExecuteResult(ControllerContext context) { if (null != context && null != context.HttpContext && null !=
public class AccessDeniedResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
if (null != context && null != context.HttpContext && null != context.HttpContext.Response)
{
context.HttpContext.Response.StatusCode = 401;
context.HttpContext.Response.RedirectToRoute("AccessDenied");
}
}
}
这不起作用,因为来自HttpResponseBase的NotImplementedException作为context.HttpContext.Response传递
如何在MVC3中编写正确的重定向操作结果?也许您应该尝试从RedirectToRouteResult继承(它继承ActionResult) 您还可以查看该类的源代码,了解它们是如何实现的:
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (context.IsChildAction) {
throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
}
string destinationUrl = UrlHelper.GenerateUrl(RouteName, null /* actionName */, null /* controllerName */, RouteValues, Routes, context.RequestContext, false /* includeImplicitMvcValues */);
if (String.IsNullOrEmpty(destinationUrl)) {
throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
}
context.Controller.TempData.Keep();
if (Permanent) {
context.HttpContext.Response.RedirectPermanent(destinationUrl, endResponse: false);
}
else {
context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
}
}
也许您应该尝试从RedirectToRouteResult继承(它继承ActionResult) 您还可以查看该类的源代码,了解它们是如何实现的:
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (context.IsChildAction) {
throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
}
string destinationUrl = UrlHelper.GenerateUrl(RouteName, null /* actionName */, null /* controllerName */, RouteValues, Routes, context.RequestContext, false /* includeImplicitMvcValues */);
if (String.IsNullOrEmpty(destinationUrl)) {
throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
}
context.Controller.TempData.Keep();
if (Permanent) {
context.HttpContext.Response.RedirectPermanent(destinationUrl, endResponse: false);
}
else {
context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
}
}
您应该返回HttpUnauthorizedResult,如下所示:
return new HttpUnauthorizedResult();
另外,您应该考虑创建一个来自AuthorizeAttribute的新类来进行安全检查。然后,您可以将此指令添加到web.config中,以控制客户端的定向位置:
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="401" redirect="~/AccessDenied" />
</customErrors>
您应该返回HttpUnauthorizedResult,如下所示:
return new HttpUnauthorizedResult();
另外,您应该考虑创建一个来自AuthorizeAttribute的新类来进行安全检查。然后,您可以将此指令添加到web.config中,以控制客户端的定向位置:
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="401" redirect="~/AccessDenied" />
</customErrors>
Comment on有错误,但没有解决方案。Comment on有错误,但没有解决方案。