如何在asp.netCore3.0中配置區(qū)域與路由-創(chuàng)新互聯(lián)

本篇文章為大家展示了如何在asp.net Core3.0中配置區(qū)域與路由,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為南城等服務(wù)建站,南城等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為南城企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一、MVC 服務(wù)注冊

ASP.NET Core 3.0 添加了用于注冊內(nèi)部的 MVC 方案的新選項Startup.ConfigureServices。


三個新的頂級擴展方法與 MVC 方案上IServiceCollection可用。 模板使用這些新方法,而不是UseMvc。 但是,AddMvc繼續(xù)像它已在以前的版本。


下面的示例將添加對控制器和與 API 相關(guān)的功能,但不是視圖或頁面的支持。 API 模板使用此代碼:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
}

下面的示例將添加對控制器、 與 API 相關(guān)的功能,和視圖,但不是頁面的支持。 Web 應(yīng)用程序 (MVC) 模板使用此代碼:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllersWithViews();
}

下面的示例添加支持 Razor 頁面和最小控制器支持。 Web 應(yīng)用程序模板使用此代碼:

public void ConfigureServices(IServiceCollection services)
{
 services.AddRazorPages();
}

此外可以組合的新方法。 下面的示例是等效于調(diào)用AddMvcASP.NET Core 2.2 中:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
 services.AddRazorPages();
}

二、Startup.Configure配置

一般不建議:


添加UseRouting。


如果該應(yīng)用程序調(diào)用UseStaticFiles,將置于UseStaticFiles之前 UseRouting。


如果應(yīng)用使用身份驗證/授權(quán)功能,如AuthorizePage或[Authorize],將對UseAuthentication并UseAuthorization后 UseRouting。


如果應(yīng)用使用CORS功能,如[EnableCors],將放置UseCors下一步。


替換UseMvc或UseSignalR與UseEndpoints。


以下是一種Startup.Configure典型的 ASP.NET Core 2.2 應(yīng)用中:

public void Configure(IApplicationBuilder app)
{
 ...

 app.UseStaticFiles();
 
 app.UseAuthentication();

 app.UseSignalR(hubs =>
 {
  hubs.MapHub<ChatHub>("/chat");
 });

 app.UseMvc(routes =>
 {
  routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
 });
}

現(xiàn)在的控制器映射內(nèi)發(fā)生UseEndpoints。

添加MapControllers如果應(yīng)用使用屬性路由。 由于路由包括對許多框架在 ASP.NET Core 3.0 或更高版本的支持,添加屬性路由的控制器是參加。

將為以下內(nèi)容:


MapRoute 使用 MapControllerRoute


MapAreaRoute 使用 MapAreaControllerRoute


由于路由現(xiàn)在包括對不止是 MVC 的支持,已更改了術(shù)語進行明確說明他們所做的這些方法。 如傳統(tǒng)路由MapControllerRoute / MapAreaControllerRoute / MapDefaultControllerRoute它們要添加的順序應(yīng)用。 將第一位更具體的路由 (如某一區(qū)域的路由)。


如下示例中:


  • MapControllers 添加了對屬性路由的控制器支持。

  • MapAreaControllerRoute 將控制器的傳統(tǒng)路由添加區(qū)域。

  • MapControllerRoute 添加控制器的常規(guī)路由。

現(xiàn)在映射 Razor 頁面內(nèi)發(fā)生UseEndpoints。


添加MapRazorPages如果應(yīng)用使用 Razor 頁面。 由于終結(jié)點路由包括對許多框架的支持添加 Razor 頁面現(xiàn)在參加。

更新后asp.netCore3.0中Startup.Configure代碼:

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 /tupian/20230522/ref=aka&shorturl=aspnetcore-hsts
    app.UseHsts();
   }

   app.UseHttpsRedirection();
   app.UseStaticFiles();

   app.UseCookiePolicy();

   app.UseRouting();

   app.UseAuthorization();

   app.UseEndpoints(endpoints =>
   {
    endpoints.MapControllerRoute(
     name: "default",
     pattern: "{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
     name: "areas", "areas",
     pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
   });
  }

如果要進行分區(qū)路由,需要在控制器Controller頭加Area和Route標簽,否則不能像在asp.netCore2.0中自動路由控制器和Action。示例代碼如下:

namespace WebApplication1.Areas.CMS.Controllers
{
 [Area("CMS")]
 [Route("CMS/[controller]/[action]")]
 public class NewsController : Controller
 {
  public IActionResult Index()
  {
   return View();
  }
  public IActionResult List()
  {
   return View();
  }
 }
}


ASP.NET 是什么

ASP.NET 是開源,跨平臺,高性能,輕量級的 Web 應(yīng)用構(gòu)建框架,常用于通過 HTML、CSS、JavaScript 以及服務(wù)器腳本來構(gòu)建網(wǎng)頁和網(wǎng)站。

上述內(nèi)容就是如何在asp.net Core3.0中配置區(qū)域與路由,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標題:如何在asp.netCore3.0中配置區(qū)域與路由-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article44/cdgsee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計、云服務(wù)器網(wǎng)站導(dǎo)航、小程序開發(fā)定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)