Back to: ASP.NET MVC Tutorial For Beginners and Professionals
ASP.NET MVC Attribute Routing with Optional Parameter
In this article, I am going to discuss the ASP.NET MVC Attribute Routing with Optional Parameter. Please read our previous article before proceeding to this article as we are going to work with the same example that we started in our previous article. In our previous article, we discuss the basics of Attribute Routing in the ASP.NET MVC Application.
ASP.NET MVC Attribute Routing with Optional Parameter:
You can also define a URI parameter as optional by adding a question mark (“?”) to the route parameter. You can also specify the default value by using parameter = value. If this is not clear at the moment, then don’t worry, we will see both these approaches with examples.
To understand this concept, let’s add a controller within the controller folder with the name as HomeController and then copy and paste the below codes. As you can see, in the MVC action method, we place a question mark after the parameter studentName within the Route attribute as [Route(“MVCTest/{studentName ?}”)] which makes the studentName parameter optional. If you have not specified any value for the studentName parameter in the URL, then it will store a null value in it and if you specified any value in the URL for the studentName parameter, then that value will be mapped to the studentName Parameter. In the WEBAPI action method, we have specified a default value for the studentName Parameter directly in the Route attribute as Route(“WEBAPITest/{studentName = Pranaya}”)].
namespace AttributeRoutingDemoInMVC.Controllers { public class HomeController : Controller { // Optional URI Parameter // URL: /MVCTest/ // URL: /MVCTest/Pranaya [Route("MVCTest/{studentName ?}")] public ActionResult MVC(string studentName) { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } // Optional URI Parameter with default value // URL: /WEBAPITest/ // URL: /WEBAPITest/Pranaya [Route("WEBAPITest/{studentName = Pranaya}")] public ActionResult WEBAPI(string studentName) { ViewBag.Message = "Welcome to ASP.NET WEB API!"; return View(); } } }
In the above example, both /MVCTest and /MVCTest/Pranaya will route to the “MVC” action method, similarly, /WEBAPITest and /WEBAPITest/Pranaya will route to the “WEBAPI” action method. Then add the below two views:
MVC.cshtml
@{ ViewBag.Title = "MVC"; } @ViewBag.Message
WEBAPI.cshtml
@{ ViewBag.Title = "WEBAPI"; } @ViewBag.Message
Now run the application and navigate to the below four URLs and see everything is working as expected.
- URL1: http://localhost:58316/MVCTest
- URL2: http://localhost:58316/MVCTest/Pranaya
- URL3: http://localhost:58316//WEBAPITest
- URL4: http://localhost:58316//WEBAPITest/Pranaya
In the next article, I am going to discuss Route Prefix in ASP.NET MVC Attribute Routing with Examples. Here, in this article, I try to explain the ASP.NET MVC Attribute Routing with Optional Parameter. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.
goog
please provide complete details ,, while working with option parameter in route parameter also need to mark with question mark
[Route(“WEBAPITest/{studentName = Pranaya}”)]
public ActionResult WEBAPI(string studentName)
{
ViewBag.Message = “Welcome to ASP.NET WEB API!”;
return View();
}
in above example need to correct
public ActionResult WEBAPI(string? studentName) then it will work
CS8370 Feature ‘nullable reference types’ is not available in C# 7.3. Please use language version 8.0 or greater.
How to fix this?
Do NOT put a space between the parameter name and the question mark, or ActionContext.RouteData will contain keys with a space after them.