current position:Home>How does the core MVC razorpages page access the backend method?

How does the core MVC razorpages page access the backend method?

2022-02-02 21:49:51 CSDN Q & A

My directory structure is as follows :


I built a Razor page , How does this page request his own PageModel Inside getString How ??

Back end :


front end :


Confused for several days , That's true. url Access controller , But this Razor page I mean, I brought my own backstage , So how to request the back-end method from the front-end ..

Refer to the answer 1:

It seems that your project uses both MVC The controller also uses Razor Pages. If you want to be in MVC Use... In projects Razor Pages, First of all, you can't forget to add the right Razor Pages Routing support :

public void ConfigureServices(IServiceCollection services)    {        services.AddControllersWithViews();        services.AddRazorPages();  // Add this     }    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)    {        //....        app.UseStaticFiles();        app.UseRouting();        app.UseAuthorization();        app.UseEndpoints(endpoints =>        {            endpoints.MapControllerRoute(                    name: "default",                    pattern: "{controller=Home}/{action=Index}/{id?}");            endpoints.MapRazorPages();  // Add this         });    }

secondly , Put it in Views Under the folder is the default MVC The routing ,Razor Pages By default, route reading is in the root directory Pages Under the folder , It is recommended that you create a... In the root directory of your project Pages Folder , That is to say, with Views also Controllers The folder is at the same level , Then put your Table All files in the folder include Table The folders themselves are all moved to Pages Under the folder (Pages/Table/myHome.cshtml), Remember, don't forget to look at your PageModel Of namespace(namespace ProjectName.Pages.Table) Isn't it .

One more question ,Razor Pages Protected by anti-counterfeiting verification , You need to use FormTagHelper Inject anti-counterfeiting token into HTML Form elements or add them to your page manually @Html.AntiForgeryToken(), And then your ajax You need to add this token. Otherwise, it will be reported 400 Error of ( You can F12 Pay attention to the console error message ). For details, you can refer to the following document :

By the way, a suggestion , You don't seem to know much about RazorPages The overall process of , I suggest you look at the document first :

Refer to the answer 2:

Refer to the answer 3:

stay Razor Pages Application ,.cshtml Is the view page , It is generally used to bind and render on the server side HTML, you HTML Medium ajax The request needs to create a separate Web API Interface , Take your project structure as an example , stay Controllers Folder created specifically for Web API Interface controller , Then create the interface service in the controller , as follows :

establish TableController.cs controller :


stay TableContrller.cs Create... In the controller Web API Interface services /api/table/list, as follows :


Last , stay Index.cshtml Call in :


The operation effect is as shown in the figure :


Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended