MVC4 Razor视图中出现奇怪的空格 [英] Strange whitespace appearing in MVC4 Razor View

查看:144
本文介绍了MVC4 Razor视图中出现奇怪的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.Net MVC4中开发一个应用程序,并且在空白方面遇到了一个奇怪的问题.没有这个问题,我已经用Razor开发了许多MVC3网站.

这是我的模板cshtml文件:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>HF - Content Management - @ViewBag.Title</title>
    <link href="@Url.Content("~/content/bootstrap/bootstrap.min.css")" rel="stylesheet" />
    @Styles.Render("~/bundles/css/hf-cms-logged-in")
</head>
    <body>

        @Html.Partial("Partials/NavBar")

        <div class="container">@RenderBody()</div>

        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="@Url.Content("~/scripts/bootstrap/bootstrap.min.js")"></script>
        @Scripts.Render("~/bundles/js/validation")
        <script src="@Url.Content("~/scripts/hf-cms.js")"></script>
    </body>
</html>

请注意带有RenderBody()调用的行-这里没有多余的空格.

当我调用一个动作时,渲染的主体前面有一些空白,我看不到自己添加的空白,而且似乎也无法摆脱.我称没有逻辑的动作,它只会返回以下视图:

@{
    ViewBag.Title = "Dashboard";
}    
<h1>Dashboard</h1>

肯定使用了正确的模板(在_ViewStart.cshtml中指定)

在Google Chrome浏览器中查看页面时,源会显示额外的空格.参见下图:

在IE10中可以看到类似的问题.这显然影响了设计.我尝试使用 Meleze.Web 去除任何多余的空白,但空白仍然存在./p>

我对此感到茫然,因为到目前为止这是一个相对简单的网站,目前还没有时髦的东西,所以我看不到这个空白来自哪里.

以前有人用MVC4或Razor看过吗?

我尝试删除所有样式表和脚本文件,但空白仍然存在.

解决方案

经过一段时间的努力,我找到了解决方案.

根_ViewStart.cshtml文件中肯定有一个有趣的字符.我删除了ViewStart文件的内容并重新输入它,从而解决了该问题.

Note the line with the RenderBody() call - there's no extraneous whitespace here.

When I call an action, the rendered body is prepended with some whitespace which I can't see that I've added, and can't seem to get rid of. I call an action with no logic, it only returns the following view:

@{
    ViewBag.Title = "Dashboard";
}    
<h1>Dashboard</h1>

It's definitely using the correct template (specified in my _ViewStart.cshtml)

Viewing the page in Google Chrome, the source shows extra whitespace. See the image below:

A similar issue can be seen in IE10. This is obviously affecting the design. I've tried using Meleze.Web to strip out any extra whitespace, but whitespace still remains.

I'm at a loss with this one, as it's a relatively simple site so far, there's nothing funky going on yet, so I can't see where this whitespace is coming from.

Has anybody else seen this with MVC4 or Razor before?

Edit: I've tried removing all stylesheets and script files, the whitespace still exists.

解决方案

After struggling with this for a while, I've found the solution.

There must have been a funny character in the root _ViewStart.cshtml file. I deleted the contents of the ViewStart file and retyped it, which solved the problem. This got me thinking that a strange character could be causing the issue.

Don't like answering my own question, but I hope this helps somebody else. In theory, this won't be an MVC4-specific issue, you could encounter the same problem in MVC3.

这篇关于MVC4 Razor视图中出现奇怪的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆