Adding “charset” to all ASP.NET MVC HTTP responses

前端 未结 3 500
醉话见心
醉话见心 2021-02-07 21:58

Is there an easy way to specify all \"normal\" views is an ASP.NET MVC app are to have charset=utf-8 appended to the Content-Type? View()

3条回答
  •  春和景丽
    2021-02-07 22:35

    You could write an attribute for it:

    public class CharsetAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Headers["Content-Type"] += ";charset=utf-8";
        }
    }
    

    Feel free to make it a bit smarter, but that's the general idea. Add it to your base controller class and your whole app is covered.

提交回复
热议问题