ASP.NET Core    MVC 6    Web Api    Route  
▌Introduction
In
this article, we will know how to 
l   Set an Areas routing
rule (template)
l   Use Route attribute in
MVC
l   Use Route attribute in
WebApi
▌Environment
l   Visual Studio 2015
Update 3
l   Visual Studio 2015
DotNetCore tools – preview 2
l   DotNetCore 1.0.0
Runtime
▌Implement
▋Set an Areas routing rule (template)
We can find
the default route template in Startup.cs
: Configure
Add the
following red codes to support Area routing ruls.
| 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    //…Skip 
app.UseMvc(routes => 
            { 
                routes.MapRoute( 
                    name:"areaRoute",  
                    template:"{area:exists}/{controller=Home}/{action=Index}/{id?}"); 
               
  routes.MapRoute( 
                   
  name: "default", 
                   
  template: "{controller=Home}/{action=Index}/{id?}"); 
           
  }); 
} | 
▋Enable Areas routing in MVC:Controller
Now create
Areas, for example,
To enable
Areas routing, use [Area] and [Route] attributes like this,
| 
[Area("User")] 
[Route("User/[controller]")] 
public class AuthController : Controller 
{ 
       [Route("[action]")] 
        public IActionResult Index() 
        { 
           
  _logger.Debug($"Areas:User,Controller:Auth,Action:Index"); 
           
  ViewBag.Title = "Index"; 
            return View(); 
        } 
        [Route("[action]")] 
        public IActionResult Create() 
        { 
           
  _logger.Debug($"Areas:User,Controller:Auth,Action:Create"); 
           
  ViewBag.Title = "Create"; 
            return View(); 
        } 
        //[Route("[action]/{page}")] //It's ok to
  write like this 
        [Route("[action]/{page:int?}")] 
        public IActionResult Edit(int? page) 
        { 
           
  _logger.Debug($"Areas:User,Controller:Auth,Action:Edit,
  and page is {page}"); 
            ViewBag.Title
  = "Edit"; 
           
  ViewBag.Page = page; 
            return View(); 
        } 
        [Route("[action]/{data}")] 
        public IActionResult Edit(String data) 
        { 
           
  _logger.Debug($"Areas:User,Controller:Auth,Action:Edit,
  and data is {data}"); 
           
  ViewBag.Title = "Edit"; 
           
  ViewBag.Page = data; 
            return View(); 
        } 
} | 
Note :
Use [Route("[action]/{page:int?}")] to add a int? URI parameter for following usage:
and  [Route("[action]/{data}")] to add a string type
URI parameter.
▋Enable Areas routing in WebApi:Controller
There is no
need to add routing template in WebApi Startup.cs
Just put
[Route] attribute to assign the routing rules for WebApi’s controllers and
actions (Restful methods).
| 
[Route("api/User/[controller]")] 
public class AuthController : Controller 
{ 
        [HttpGet("GetAll")] 
        public IEnumerable<string> Get() 
        { 
            return new string[] { "auth1", "auth2" }; 
        } 
        [HttpGet("Get/{id}")] 
        public string Get(int id) 
        { 
            return id.ToString(); 
        } 
        // POST api/values 
        [HttpPost] 
        public void Post(DtoModel model) 
        { 
        } 
        // DELETE api/values/5 
        [HttpDelete("Delete/{id}")] 
        public void Delete(int id) 
        { 
        } 
    } | 
Note that you
can add an action name in Http Method attribute like the following to define
the action name for the action 
[HttpGet("GetAll")]
public
IEnumerable<string> Get()
And the
HttpGet url will be 
http://localhost:14125/api/user/auth/getall
Results in
Postman:
▌Reference





 
沒有留言:
張貼留言