为什么我的ASP.NET Core View出现编码错误? [英] Why am I getting a encoding error with my ASP.NET Core View?

查看:47
本文介绍了为什么我的ASP.NET Core View出现编码错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近进入了ASP.NET Core,正在研究一些文档,但是有一件事情使我无法找到解决方案.当我运行我的应用程序时,在控制台中出现以下错误:

I've recently stepped into ASP.NET Core and I'm working through some documentation but one thing has cropped up more than one that I can't find the solution too. When I run my application I get the following error in the console:

未声明纯文本文档的字符编码.该文档将在某些浏览器中显示乱码如果文档包含来自外部的字符,则进行配置US-ASCII范围.文件的字符编码需要为在传输协议或文件中声明的内容需要使用字节顺序标记为编码签名.

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

当我搜索此错误时,我看到消息指出应该将基本编码声明添加到文档头.那是有道理的,但是没有用.

When I searched on this error I saw messages stating that I should add the base encoding declaration to the document head. That made sense but didn't work.

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

这是我当前的设置:

HomeController

using Microsoft.AspNetCore.Mvc;
using Senua.Models;
using System.Diagnostics;

namespace Senua.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

视图/主页/Index.cshtml

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

视图/共享/布局

为简洁起见,我已将其简化.

I've shortened this for brevity.

<!DOCTYPE html>
<html>
<head>

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>@ViewData["Title"] - WebApplication1</title>

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
    </environment>
</head>
<body>
    <partial name="_CookieConsentPartial" />
    <div class="container body-content">
        @RenderBody()
    </div>

    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa">
        </script>
        <script src="~/js/site.min.js" asp-append-version="true"></script>
    </environment>

    @RenderSection("Scripts", required: false)
</body>
</html>

Views/ViewStart.cshtml

@{
    Layout = "_Layout";
}

Views/ViewIMports.cshtml

@using Senua
@using Senua.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Senua.DAL;

namespace Senua
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<LocalContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("Melina")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

简而言之,这就是设置.据我所知,ViewStart定义了布局,该布局应交付文档设置,然后渲染体就位以从主页交付内容.Home具有返回视图的控制器和方法.我想念什么?

So that's the setup in a nutshell. As far as I can see the ViewStart defines layout which should deliver the document setup and then renderbody is in place to delivery content from the homepage. Home has it's controller and method to return the view. What am I missing?

非常感谢

推荐答案

我进行了一些代码检查,发现问题出在 Startup.cs .虽然我已经定义了 app.UseMVC(),但是我没有为默认路径设置路由.更改此设置使我可以使用视图,并且可以正确访问控制器.

I did a little more code examination and discovered that the problem was coming from the Startup.cs. Whilst I had defined app.UseMVC() I had not set the route for the default path. Changing this allowed me to work with views and the controller to be accessed correctly.

Startup.cs(原始)

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }

Startup.cs(修订版)

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

这篇关于为什么我的ASP.NET Core View出现编码错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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