问题
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.
回答1:
- 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.
来源:https://stackoverflow.com/questions/63099635/asp-net-core-applications-issue-with-razor-and-mvc-pages