Razor 和 MVC 页面的 Asp.net 核心应用程序问题 [英] Asp.net Core Applications Issue with Razor and MVC Pages
问题描述
我已经重新安装了我的 VS 2019 并在我的机器上克隆了我的应用程序.但是突然所有应用程序都停止显示添加 ->控制器 -->MVC 5 控制器及其视图.重建后,我的应用程序从 MVC Core 3.1 转换为 Razor.现在所有应用程序都会抛出 g.cshtml.cs 文件的错误.帮我解决这个问题.
- 为什么我的 .net 核心 MVC 应用程序被转换为 Razor.
- 为什么应用程序不添加 MVC 控制器和视图.
- 创建一个空的 ASP.NET Core MVC 项目
将Endpoint
路由改为MVC
路由.
public void ConfigureServices(IServiceCollection services){//services.AddControllersWithViews();#region 2.2 MVCRouterConfigureservices.AddMvc(options =>{options.EnableEndpointRouting = false;}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);#endregion}//这个方法被运行时调用.使用此方法配置 HTTP 请求管道.公共无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment 环境){如果 (env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/Home/Error");//默认的 HSTS 值为 30 天.您可能希望针对生产场景更改此设置,请参阅 https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();//app.UseRouting();app.UseAuthorization();app.UseMvc();//app.UseEndpoints(endpoints =>//{//endpoints.MapControllerRoute(//名称:默认",//模式:{controller=Home}/{action=Index}/{id?}");//});}
<块引用>
- Razor 页面的设置
现在您已经创建了项目,让我们准备它以使用 Razor Pages
.
首先在项目的根文件夹下创建一个名为 Pages
的文件夹.默认情况下,razor 页面存储在 Pages 文件夹中,可以从浏览器中以 Pages 为根访问.例如,如果您将 Index.cshtml
放在 Pages 文件夹中,那么它可以作为 https://localhost:44366/Index
添加剃刀页面.右键单击 Pages 文件夹,然后选择 Add
>新项目
.选择 Razor Page 项并将名称指定为 Index.cshtml
.单击添加
按钮.您将看到 Pages
文件夹中的两个文件 - Index.cshtml
和 Index.cshtml.cs
.
您可以在 Pages 文件夹下创建更多文件夹树.根据页面的位置,其 URL 会发生变化.例如,如果您将 Hello.cshtml
存储在 /Pages/Test
下,那么您可以通过 http://localhost:12345/Test/Hello
您可以从这里看到的详细信息.
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.
- Why my .net core MVC application is converted to Razor.
- Why application not adding MVC controller and view.
- Create an empty ASP.NET Core MVC project
Change Endpoint
routing to MVC
routing.
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?}");
//});
}
- Setup for Razor Pages
Now that you have created the project, let's prepare it to use Razor Pages
.
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
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.
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屋!