[英]Asp.net Core Applications Issue with Razor and MVC Pages
我已经重新安装了我的 VS 2019 并在我的机器上克隆了我的应用程序。 但突然所有应用程序都停止显示 add -> Controller --> MVC 5 Controller 及其视图。 重建后,我的应用程序从 MVC Core 3.1 转换为 Razor。 现在所有应用程序都会抛出 g.cshtml.cs 文件的错误。 帮我解决这个问题。
- 创建一个空的 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?}");
//});
}
- Razor 页面的设置
现在您已经创建了项目,让我们准备使用Razor Pages
。
首先在项目的根文件夹下创建一个名为Pages
的文件夹。 默认情况下,razor 页面存储在 Pages 文件夹中,并且可以从以 Pages 为根目录的浏览器访问。 例如,如果您将Index.cshtml
放在 Pages 文件夹中,则可以通过https://localhost:44366/Index
访问它
添加 razor 页面。 右键单击 Pages 文件夹,然后单击 select
Add
> New Item
。 Select Razor 页面项目并将名称指定为Index.cshtml
。 单击Add
按钮。 您将观察到两个文件 - Pages
文件夹中的Index.cshtml
和Index.cshtml.cs
。
您可以在 Pages 文件夹下创建更多文件夹树。 根据页面的位置,其 URL 将发生变化。 例如,如果您将Hello.cshtml
存储在/Pages/Test
下,则可以通过 http://localhost:12345/Test/Hello 访问它
你可以从这里看到的细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.