I've migrated from mvc 3 to mvc 4 and encountered with the following problem.
@using InvoiceDocflow.Controllers
@{
Response.ContentType = "text/xml";
}
<?xml version="1.1" encoding="UTF-8" ?>
<dc>
@foreach (var dcLink in (IEnumerable<DcLink>)ViewData["SupportedDcs"])
{
<link rel="@dcLink.RelUri.ToString()" href="@dcLink.DcUri.ToString()" />
}
</dc>
This is my view. My layout is just one line
@RenderBody()
So in mvc 3 <?xml version="1.1" encoding="UTF-8" ?>
appeared in the first line, but now, its appears on the second line, leaving th first line empty.
Can I make it render on the first line as it was in mvc 3?
By the way.
@using InvoiceDocflow.Controllers
@{
Response.ContentType = "text/xml";
}<?xml version="1.1" encoding="UTF-8" ?>
This would work, but this is not what I whant to do at all.
Razor supports C# and uses the @ symbol to transition from HTML to C#. Razor evaluates C# expressions and renders them in the HTML output.
In MVC, @ is the respective char that allows you to use razor inside HTML (inside a . cshtml) which in runtime (or precompiled) will be converted to c#. With @ you may write C# within HTML and with @: you may write HTML within C#. Example: @foreach (TestClass item in Model) { @:@item.Code - @item.Name }
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.
Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.
I ran into a similar problem with blank lines at the top of the page when trying to return a cache manifest file. Solution, Add a Response.Write("..."), this will be at the first line of the page.
@{
Layout = null;
Response.Write("CACHE MANIFEST");
} .......
Temporary fix? ActionFilter and strip out the empty first line? Clearly you could also do other minification on the response if suitable.
public class TranslationFilter : MemoryStream
{
private Stream filter = null;
public TranslationFilter(HttpResponseBase httpResponseBase)
{
filter = httpResponseBase.Filter;
}
public override void Write(byte[] buffer, int offset, int count)
{
var response = UTF8Encoding.UTF8.GetString(buffer);
// remove all newlines
response = response.Replace(System.Environment.NewLine, "");
/* remove just first empty line
if (response.Substring(0, 2) == "\r\n")
{
response = response.Substring(2, response.Length - 2);
} */
filter.Write(UTF8Encoding.UTF8.GetBytes(response), offset, UTF8Encoding.UTF8.GetByteCount(response));
}
}
public class ResponseFilter : ActionFilterAttribute
{
public ResponseFilter()
{
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
filterContext.HttpContext.Response.Filter = new TranslationFilter(filterContext.HttpContext.Response);
}
}
And add it onto the Controller method?
[ResponseFilter]
public ActionResult Index()
{
return View();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With