①首先更新VS2017为最新版,参考:
https://www.jb51.net/softjc/683226.html
https://jingyan.baidu.com/article/7f41ececa91098193d095cf1.html
②接下来添加路由设置:
添加以下代码,到 Startup.cs 文件的 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 函数里去:
app.UseMvc(routes =>{routes.MapRoute(name: "area",template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});
具体参考如下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env){app.UseMvc(routes =>{routes.MapRoute(name: "area",template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseHsts();}app.UseHttpsRedirection();app.UseMvc();}
③在你的Controller上方添加:[Area("Demo")]
namespace JSYSTEM.API.Areas.Demo.Controllers
{[Area("Demo")]public class TestController : Controller{[HttpGet]public IActionResult Index(){return View();}}
}
运行后,url定位到:https://localhost:1234/demo/test 就可以了。其中demo是区域Area,test是控制器Controller
④.NET WebAPI调用,Controller上方添加路由[Route("[area]/[controller]/[action]")] 和 [Area("Demo")]
namespace SYSTEM.API.Areas.Demo.Controllers
{ [ApiController][Area("Demo")][Route("[area]/[controller]/[action]")]public class TestApiController : BaseApiController{//https://localhost:44377/Demo/TestApi/GetStringApi[HttpGet]public ApiBaseResult GetStringApi(){return ReturnApiBaseResult("hello word!");}}
}
调用API接口:https://localhost:44377/Demo/TestApi/GetStringApi 就可以了