[英]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.
帮我解决这个问题。
- 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?}");
//});
}
- 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.cshtml
和Index.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 访问它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.