ASP .NET MVC, раутинг |
Разработка на C# для Gentoo,
|
[RoutePrefix("Users")]
public class HomeController : Controller
{
[Route("~/")] //Specifies that this is the default action for the entire application. Route: /
[Route("")] //Specifies that this is the default action for this route prefix. Route: /Users
public ActionResult Index() { ... }
}
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
tes.MapMvcAttributeRoutes(); //Enables Attribute Based Routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
);
}
The joy here is that it is easy to see the route to a controller action by looking at the RoutePrefix and Route Attributes on the controller and action. You don't have to find it somewhere else.
| Constraint | Description | Example |
|---|---|---|
| alpha | Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) | {x:alpha} |
| bool | Matches a Boolean value. | {x:bool} |
| datetime | Matches a DateTime value. | {x:datetime} |
| decimal | Matches a decimal value. | {x:decimal} |
| double | Matches a 64-bit floating-point value. | {x:double} |
| float | Matches a 32-bit floating-point value. | {x:float} |
| guid | Matches a GUID value. | {x:guid} |
| int | Matches a 32-bit integer value. | {x:int} |
| length | Matches a string with the specified length or within a specified range of lengths. | {x:length(6)} {x:length(1,20)} |
| long | Matches a 64-bit integer value. | {x:long} |
| max | Matches an integer with a maximum value. | {x:max(10)} |
| maxlength | Matches a string with a maximum length. | {x:maxlength(10)} |
| min | Matches an integer with a minimum value. | {x:min(10)} |
| minlength | Matches a string with a minimum length. | {x:minlength(10)} |
| range | Matches an integer within a range of values. | {x:range(10,50)} |
| regex | Matches a regular expression. | {x:regex(^\d{3}-\d{3}-\d{4}$)} |
{controller=Home}/{action=Index}.?: {controller=Home}/{action=Index}/{id?}.[RoutePrefix("reviews")]
public class ReviewsController : Controller {
// eg.: /reviews/5
[Route("{reviewId:int}")]
public ActionResult Show(int reviewId) { ... }
// eg.: /reviews/5/edit
[Route("{reviewId:int}/{action}")]
public ActionResult Edit(int reviewId) { ... }
}
GET /Account/ResetPasswordPOST /Account/ResetPasswordGET /Account/PasswordResetVerificationSentGET /Account/RP (by clicking on e-mail link)POST /Account/RPGET /Account/PasswordReset