In ASP.NET MVC 5, you can add headers to HTTP responses by using the Response property of the Controller base class and the AddHeader method.
Here's an example code snippet that shows how to add headers in ASP.NET MVC 5:
public class MyController : Controller { public ActionResult MyAction() { // Do some work // Add a header to the response Response.AddHeader("X-MyHeader", "MyHeaderValue"); // Return a view return View(); } } In this example, a new MyController class is defined that derives from the Controller base class. The MyAction method is defined as an action that performs some work and returns a view.
Before returning the view, the Response property of the controller is used to add a new header to the HTTP response. The AddHeader method is called with the name and value of the header to be added.
When the action is executed, the HTTP response will include the new header, which can be used to provide additional information to the client or to control how the client handles the response.
Note that you can add multiple headers to an HTTP response by calling the AddHeader method multiple times with different names and values. You can also use other methods of the Response property to set other properties of the HTTP response, such as the status code or content type.
"Add custom response headers in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { Response.AddHeader("CustomHeader", "HeaderValue"); return View(); } "Set cache control headers in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); return View(); } "Add Content-Security-Policy header in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { Response.AddHeader("Content-Security-Policy", "default-src 'self'"); return View(); } "Implement HTTP Strict Transport Security (HSTS) in ASP.NET MVC 5"
// Code Implementation (Global.asax.cs or Middleware) protected void Application_Start() { System.Web.HttpContext.Current.Response.AddHeader("Strict-Transport-Security", "max-age=31536000"); } "Add custom request headers in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { var customHeaderValue = Request.Headers["CustomHeader"]; // Process custom header value return View(); } "Set X-Frame-Options header in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { Response.AddHeader("X-Frame-Options", "DENY"); return View(); } "Add CORS headers in ASP.NET MVC 5"
// Code Implementation (Global.asax.cs or Middleware) protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type"); } "Implement custom response headers based on conditions in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { if (/* Your condition */) { Response.AddHeader("CustomHeader", "ConditionalValue"); } return View(); } "Add Expires header for static resources in ASP.NET MVC 5"
// Code Implementation (Global.asax.cs or Middleware) protected void Application_BeginRequest(object sender, EventArgs e) { if (/* Check if the request is for a static resource */) { Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetValidUntilExpires(true); } } "Add custom headers to ActionResult in ASP.NET MVC 5"
// Code Implementation (Controller Action) public ActionResult Index() { var result = View(); result.Headers.Add("CustomHeader", "HeaderValue"); return result; } libraries smtp-auth text-parsing statelesswidget window-soft-input-mode sum capture-group ascii asp.net-mvc-3-areas vector-graphics