简体   繁体   English

Asp.net 核心应用程序问题与 Razor 和 MVC 页面

[英]Asp.net Core Applications Issue with Razor and MVC Pages

I have reinstalled my VS 2019 and cloned my applications on my machine.我已经重新安装了我的 VS 2019 并在我的机器上克隆了我的应用程序。 But suddenly all the applications stopped showing add -> Controller --> MVC 5 Controller and its View.但突然所有应用程序都停止显示 add -> Controller --> MVC 5 Controller 及其视图。 After rebuild my application is converted into Razor from MVC Core 3.1.重建后,我的应用程序从 MVC Core 3.1 转换为 Razor。 and now all applications throws error of g.cshtml.cs file.现在所有应用程序都会抛出 g.cshtml.cs 文件的错误。 Help me to fix this issues.帮我解决这个问题。

  1. Why my .net core MVC application is converted to Razor.为什么我的 .net 核心 MVC 应用程序转换为 Razor。
  2. Why application not adding MVC controller and view.为什么应用程序不添加 MVC controller 并查看。
  1. Create an empty ASP.NET Core MVC project创建一个空的 ASP.NET Core MVC 项目

Change Endpoint routing to MVC routing.Endpoint路由更改为MVC路由。

    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddControllersWithViews();

        #region 2.2 MVCRouterConfigure
        services.AddMvc(options =>
        {
            options.EnableEndpointRouting = false;
        }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        #endregion
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        //app.UseRouting();

        app.UseAuthorization();

        app.UseMvc();

        //app.UseEndpoints(endpoints =>
        //{
        //    endpoints.MapControllerRoute(
        //        name: "default",
        //        pattern: "{controller=Home}/{action=Index}/{id?}");
        //});
    }
  1. Setup for Razor Pages Razor 页面的设置

Now that you have created the project, let's prepare it to use Razor Pages .现在您已经创建了项目,让我们准备使用Razor Pages

Begin by creating a folder named Pages under project's root folder.首先在项目的根文件夹下创建一个名为Pages的文件夹。 By default razor pages are stored inside Pages folder and can be accessed from the browser with Pages as their root.默认情况下,razor 页面存储在 Pages 文件夹中,并且可以从以 Pages 为根目录的浏览器访问。 For example, if you have Index.cshtml housed inside Pages folder then it can be accessed as https://localhost:44366/Index例如,如果您将Index.cshtml放在 Pages 文件夹中,则可以通过https://localhost:44366/Index访问它

在此处输入图像描述 To add a razor page.添加 razor 页面。 right click on the Pages folder and then select Add > New Item .右键单击 Pages 文件夹,然后单击 select Add > New Item Select Razor Page item and specify name as Index.cshtml . Select Razor 页面项目并将名称指定为Index.cshtml Click on the Add button.单击Add按钮。 You will observe that two files - Index.cshtml and Index.cshtml.cs in Pages folder.您将观察到两个文件 - Pages文件夹中的Index.cshtmlIndex.cshtml.cs

在此处输入图像描述

You can create further folder tree under Pages folder.您可以在 Pages 文件夹下创建更多文件夹树。 According to the page's location its URL will change.根据页面的位置,其 URL 将发生变化。 For example, if you store Hello.cshtml under /Pages/Test then you can access it at http://localhost:12345/Test/Hello例如,如果您将Hello.cshtml存储在/Pages/Test下,则可以通过 http://localhost:12345/Test/Hello 访问它

在此处输入图像描述

The detail you can see from here .你可以从这里看到的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM