Finished 'Custom Routing in ASP.NET MVC'
parent
425505c719
commit
9704647779
59
2_Routing.md
59
2_Routing.md
@ -117,3 +117,62 @@ namespace FirstMVCDemo
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Custom Routing in ASP.NET MVC App
|
||||||
|
|
||||||
|
### Creating Custom Routes in ASP.NET MVC App
|
||||||
|
|
||||||
|
* Using `MapRoute` extension method.
|
||||||
|
* Need provide at least **route name** & **URL patter**.
|
||||||
|
* Default parameter is optional
|
||||||
|
* Multiple custom routes can be created with different names
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
namespace FirstMVCDemo
|
||||||
|
{
|
||||||
|
public class RouteConfig
|
||||||
|
{
|
||||||
|
public static void RegisterRoutes(RouteCollection routes)
|
||||||
|
{
|
||||||
|
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
|
||||||
|
|
||||||
|
routes.MapRoute(
|
||||||
|
name: "Employee",
|
||||||
|
url: " Employee/{id}",
|
||||||
|
defaults: new
|
||||||
|
{
|
||||||
|
controller = "Employee",
|
||||||
|
action = "Index"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
routes.MapRoute(
|
||||||
|
name: "Default", //Route Name
|
||||||
|
url: "{controller}/{action}/{id}", //Route Pattern
|
||||||
|
defaults: new
|
||||||
|
{
|
||||||
|
controller = "Home", //Controller Name
|
||||||
|
action = "Index", //Action method Name
|
||||||
|
id = UrlParameter.Optional //Defaut value for above defined parameter
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Following URLs will be mapped to Employee route:
|
||||||
|
* `http://localhost:xxxxx/Employee`
|
||||||
|
* `http://localhost:xxxxx/Employee/Index`
|
||||||
|
* `http://localhost:xxxxx/Employee/Index/3`
|
||||||
|
|
||||||
|
Rules of creating route:
|
||||||
|
* Always put more specific route on top
|
||||||
|
|
||||||
|
## Route Constraints in ASP.NET MVC
|
||||||
|
|
||||||
|
|
||||||
|
## Attribute Routing in ASP.NET MVC
|
||||||
|
|
||||||
|
## Attribute Routing with Optional Parameter
|
||||||
|
|
||||||
|
## Route Prefix in Attribute Routing
|
Loading…
x
Reference in New Issue
Block a user