- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用本地化 actionfilterattribute,它工作得很好,除了我需要它从 /
重定向到 /en
状态代码为 301
而不是 302
。我该如何解决这个问题?
public class Localize : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// .. irrelevent logic here ..
// Set redirect code to 301
filterContext.HttpContext.Response.Status = "301 Moved Permanently";
filterContext.HttpContext.Response.StatusCode = 301;
// Redirect
filterContext.Result = new RedirectResult("/" + cookieLanguage);
base.OnActionExecuting(filterContext);
}
}
最佳答案
您可以创建自定义操作结果来执行永久重定向:
public class PermanentRedirectResult : ActionResult
{
public string Url { get; private set; }
public PermanentRedirectResult(string url)
{
this.Url = url;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.StatusCode = 301;
response.Status = "301 Moved Permanently";
response.RedirectLocation = Url;
response.End();
}
}
您可以用来执行重定向:
public class Localize : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// .. irrelevent logic here ..
filterContext.Result = new PermanentRedirectResult("/" + cookieLanguage);
}
}
关于c# - 如何设置 ActionExecutingContext 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16629029/
我想确定请求是基于 ajax 还是来自 ActionExecutingContext 对象的简单请求。这个可以识别吗?我使用 C# 和 Asp.Net MVC 2 最佳答案 您可以使用filterCo
我需要通过 http 方法和路由模板限制对 Controller 方法的访问。如何从 context var 获取路由模板? public abstract class BaseController
是否可以从 ActionExecutingContext 对象获取源端口?如果是,怎么办? 最佳答案 对,看ActionExecutingContext.HttpContext.Request.Url
我正在使用本地化 actionfilterattribute,它工作得很好,除了我需要它从 / 重定向到 /en 状态代码为 301 而不是 302。我该如何解决这个问题? 代码 public cla
给出以下代码: public class MyActionFilter : ActionFilterAttribute { public override void OnActionExecu
当我向 api Controller 添加过滤器属性时,它不起作用,因为使用了 ActionExecutingContext。当我找到必须使用 HttpActionContext 的解决方案时,它工作
我正在编写一个 ActionFilter 并且需要从 ActionExecutingContext 参数中获取区域名称(我想实现基于快速和脏登录的安全性)。是否可以? 最佳答案 用法 @Html.Co
如何访问 ActionExecutingContext 中的 Request.Properties? public class UserFilter : ActionFilterAttribute {
只是好奇,它们听起来很相似。 ExecuteCore() 与 OnActionExecuting(ActionExecutingContext filterContext) 之间有什么区别? 在什么情
你好,突然我的 mvc 应用程序开始出现异常: rror 36 The type or namespace name 'ActionExecutingContext' could not be
我正在尝试测试以下过滤器: using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Filters; namespace Hello {
如果我有一个 ActionExecutingContext 实例,我如何获取该 ActionExecutingContext 的 MethodInfo有问题的操作吗? 最佳答案 ActionExecu
正如您在下面看到的,在我的 ActionFilter 中,我尝试获取 ActionExecutingContext.ActionDescriptor 的 ActionName 和 MethodInfo
我正在努力实现自定义的 AuthorizeAttribute。 AuthorizeCore 覆盖接受 HttpContextBase。如果用户没有正确的角色,那么我想抛出一个错误。我找到了一些代码,我
如果我正在编写这样的 ActionFilter public override void OnActionExecuting(ActionExecutingContext filterContext)
这曾经用于早期版本的 .Net。 .net 核心术语中的等价物是什么。现在我收到以下错误: “ActionDescriptor”不包含“GetCustomAttributes”的定义,并且找不到接受“
我的应用程序上有一个 ActionFilterAttribute,用于在检测到用户未通过身份验证时重定向用户。在过滤器中,我想检测操作的 ReturnType 何时为 JsonResult。 作为解决
我不确定我遗漏了什么,但我真的无法引用 OnActionExecuting(ActionExecutingContext filterContext) 下的 User.Identity using S
我正在向 Controller 发送 AES 加密请求正文,以下是一个示例: (using crypto-js) {body: "U2FsdGVk186Jj7LySqT966qTdpQZwiR+wR0
我是一名优秀的程序员,十分优秀!