Razor和MVC页面的Asp.net核心应用程序问题 [英] Asp.net Core Applications Issue with Razor and MVC Pages

查看:59
本文介绍了Razor和MVC页面的Asp.net核心应用程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重新安装了VS 2019并在我的计算机上克隆了我的应用程序.但是突然所有的应用程序停止显示add->控制器->MVC 5控制器及其视图.重建后,我的应用程序从MVC Core 3.1转换为Razor.现在所有应用程序都会引发g.cshtml.cs文件错误.帮助我解决此问题.

I have reinstalled my VS 2019 and cloned my applications on my machine. But suddenly all the applications stopped showing add -> Controller --> MVC 5 Controller and its View. After rebuild my application is converted into Razor from MVC Core 3.1. and now all applications throws error of g.cshtml.cs file. Help me to fix this issues.

  1. 为什么将.net核心MVC应用程序转换为Razor.
  2. 为什么应用程序未添加MVC控制器和视图.

推荐答案

  1. 创建一个空的ASP.NET Core MVC项目

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. 设置剃须刀页面

现在您已经创建了项目,让我们准备好使用 Razor Pages .

Now that you have created the project, let's prepare it to use Razor Pages.

首先在项目的根文件夹下创建一个名为 Pages 的文件夹.默认情况下,剃须刀页面存储在Pages文件夹中,并且可以以Pages为根从浏览器进行访问.例如,如果您在文件夹中放置了 Index.cshtml ,则可以将其作为 https://localhost:44366/Index

Begin by creating a folder named Pages under project's root folder. By default razor pages are stored inside Pages folder and can be accessed from the browser with Pages as their root. For example, if you have Index.cshtml housed inside Pages folder then it can be accessed as https://localhost:44366/Index

添加剃须刀页面.右键单击Pages文件夹,然后选择 Add >新商品.选择剃刀页面"项,并将名称指定为 Index.cshtml .点击添加按钮.您将观察到两个文件- Pages 文件夹中的 Index.cshtml Index.cshtml.cs .

To add a razor page. right click on the Pages folder and then select Add > New Item. Select Razor Page item and specify name as Index.cshtml. Click on the Add button. You will observe that two files - Index.cshtml and Index.cshtml.cs in Pages folder.

您可以在Pages文件夹下创建其他文件夹树.根据页面的位置,其URL将更改.例如,如果将 Hello.cshtml 存储在/Pages/Test 下,则可以在http://localhost:12345/Test/Hello

You can create further folder tree under Pages folder. According to the page's location its URL will change. For example, if you store Hello.cshtml under /Pages/Test then you can access it at http://localhost:12345/Test/Hello

您可以从此处看到的详细信息

The detail you can see from here.

这篇关于Razor和MVC页面的Asp.net核心应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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