在剃刀mvc 4 rc清空第一行

前端之家收集整理的这篇文章主要介绍了在剃刀mvc 4 rc清空第一行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经从mvc 3迁移到mvc 4并遇到了以下问题.
@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>

这是我的观点.我的布局只有一行

@RenderBody()

所以在mvc 3<?xml version =“1.1”encoding =“UTF-8”?>出现在第一行,但现在,它出现在第二行,第一行留空.

我可以在第一行渲染它,因为它在mvc 3中吗?

顺便说说.

@using InvoiceDocflow.Controllers
@{
    Response.ContentType = "text/xml";
}<?xml version="1.1" encoding="UTF-8" ?>

这可行,但这不是我想做的事情.

解决方法

临时修复? ActionFilter并删除空的第一行?显然,如果合适,您还可以对响应进行其他缩小.
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);
    }
}

并将其添加到Controller方法

[ResponseFilter]
public ActionResult Index()
{
return View();
}
原文链接:https://www.f2er.com/aspnet/249118.html

猜你在找的asp.Net相关文章