简体   繁体   中英

MVC.Net Controller path with multiple parameters

[Route("add/user/{name}&{state}&{zipcode}&{indeFlag}&{email}")]     
public async Task<ActionResult> CreateUser(
                 string name,
                 string state,
                 string zipcode,
                 Boolean indeFlag,
                 string email)
{
}

How to define Route of the controller method in the example above to that I can pass correct data to the method?

Please help.

You can pass each parameter like this :

[Route("[controller]")]
public class UserController : Controller
{

     // < MVC 6 :

     [Route("add")] 
     [HttpPost]    
     public async Task<IActionResult> Create(
                 string name,
                 string state,
                 string zipcode,
                 bool indeFlag,
                 string email)
     {
         // Your code here
     }

     // If you want call this in a simple get query
     [Route("quickadd")] 
     [HttpGet]    
     public async Task<IActionResult> Create(
                 string name,
                 string state,
                 string zipcode,
                 bool indeFlag,
                 string email)
     {
         // Your code here
     }

     // MVC 6 :

     [Route("add")] 
     [HttpPost]    
     public async Task<IActionResult> Create(
                 [FromForm]string name,
                 [FromForm]string state,
                 [FromForm]string zipcode,
                 [FromForm]bool indeFlag,
                 [FromForm]string email)
     {
         // Your code here
     }

     // If you want call this in a simple get query
     [Route("quickadd")] 
     [HttpGet]    
     public async Task<IActionResult> QuickAdd(
                 [FromQuery]string name,
                 [FromQuery]string state,
                 [FromQuery]string zipcode,
                 [FromQuery]bool indeFlag,
                 [FromQuery]string email)
     {
         // Your code here
     }

}

The url will be '/user/add' with data in form.

Other solution, better by the way, is to use a view model class, like this :

[Route("[controller]")]
public class UserController : Controller
{

     [Route("add")] 
     [HttpPost]    
     public async Task<IActionResult> Create(
                 AddViewModel model)
     {
         if (ModelState.IsValid)
         {
              // Put your code to create user here
         }
         else
         {
              // Put your code for error here
         }
     }

}

A view model allows you to use Data Annotation to do some checks, like required fields, or format checks.

using System.ComponentModel.DataAnnotations;

public class AddViewModel
{
     [Required(ErrorMessage = "Please enter a name")]
     [Display(Name = "Username")]
     [DataType(DataType.Text)]
     public string Name { get; set; }

     public string State{ get; set; }

     [Required]
     [DataType(DataType.EmailAddress)]
     public string Email{ get; set; }

     [Required]
     [DataType(DataType.PhoneNumber)]
     public string PhoneNumber{ get; set; }

     public AddViewModel()
     {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM