]> Visual Studio Browser Link and XHTML 🌐:aligrant.com

Visual Studio Browser Link and XHTML

Alastair Grant | Thursday 27 July 2017

XHTML support in MVC5 seems to be largely lacking. Older ASP code used to generate XHTML web-sites, but MVC spits out text/html. So far I haven't found a default toggle to enable it either.

Instead I've added an attribute to my controller classes called [XhtmlFilter] which adjusts the content type appropriately.

public class XhtmlFilterAttribute : FilterAttribute, IResultFilter
	{
		public void OnResultExecuted(ResultExecutedContext filterContext)
		{
			switch(filterContext.HttpContext.Response.ContentType.ToLower())
			{
				case "text/html":
					filterContext.HttpContext.Response.ContentType = "application/xhtml+xml";
					break;
			}
		}

		public void OnResultExecuting(ResultExecutingContext filterContext) { }
	}

This seems to to the job. But I'm yet again having a problem with Browser Link - that wonderful feature that makes CSS development a breeze by instantly reflecting your changes as you type.

The cause, serving up XHTML compliant code with the different content-type. When you do this, the browser link JavaScript simply doesn't get injected into the output and doesn't even try to run. I've logged this as an issue against Visual Studio, but I hold little hope that Microsoft will actually do anything about it.

In the mean time, you have to turn off correct Content-Type when doing CSS development.

 

Breaking from the voyeuristic norms of the Internet, any comments can be made in private by contacting me.