加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在剃刀mvc 4 rc清空第一行

发布时间:2020-12-15 22:24:52 所属栏目:asp.Net 来源:网络整理
导读:我已经从mvc 3迁移到mvc 4并遇到了以下问题. @using InvoiceDocflow.Controllers@{ Response.ContentType = "text/xml";}?xml version="1.1" encoding="UTF-8" ?dc @foreach (var dcLink in (IEnumerableDcLink)ViewData["SupportedDcs"]) { link rel="@dcLi
我已经从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) == "rn")
        {
            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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读