Finished 'Attribute Routing with Optional Parameter'
parent
bda1f2ec47
commit
7a9d3dc187
27
2_Routing.md
27
2_Routing.md
@ -293,8 +293,31 @@ Now `http://localhost:xxxx/students/2/courses` will be routed to `GetStudentCour
|
||||
* controller action method have `[Route]` attribute use Attribute Routing
|
||||
* controller action method without `[Route]` attribute uses Convention-based routing.
|
||||
|
||||
## Attribute Routing in ASP.NET MVC
|
||||
|
||||
## Attribute Routing with Optional Parameter
|
||||
|
||||
URI parameter can also be defined in attribute routing by adding **question mark ("?")** to router parameter within `{}`. We can also specify default value by **parameter = value**
|
||||
|
||||
e.g. optional URI Parameter `{studentName? }`
|
||||
|
||||
```csharp
|
||||
// Optional URI Parameter
|
||||
// URL: /MVCTest/
|
||||
// URL: /MVCTest/Pranaya
|
||||
[Route("MVCTest/{studentName ?}")]
|
||||
public ActionResult MVC(string studentName)
|
||||
{
|
||||
DoSth(studentName);
|
||||
return View();
|
||||
}
|
||||
// Optional URI Parameter with default value
|
||||
// URL: /WEBAPITest/
|
||||
// URL: /WEBAPITest/Pranaya
|
||||
[Route("WEBAPITest/{studentName = Pranaya}")]
|
||||
public ActionResult WEBAPI(string studentName)
|
||||
{
|
||||
DoSth(studentName);
|
||||
return View();
|
||||
}
|
||||
```
|
||||
|
||||
## Route Prefix in Attribute Routing
|
Loading…
x
Reference in New Issue
Block a user