gpt4 book ai didi

razor - Razor MVC 4 RC中的空第一行

转载 作者:行者123 更新时间:2023-12-04 04:55:33 25 4
gpt4 key购买 nike

我已经从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();
}

关于razor - Razor MVC 4 RC中的空第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11327787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com