Benutzer:MovGP0/ASP.NET Core/Routing
| MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | MSCert | Physik |
|
|
Routing in ASP.NET Core
OWIN
- Default route
// app.UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); });
app.UseMvcWithDefaultRoute();
- Route Builder
var routes = new RouteBuilder(app);
routes.DefaultHandler = new MvcRouteHandler(...);
routes.MapRoute("blog", "blog/{*article}", defaults: new { controller = "Blog", action = "Article" });
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); // default route
var routeCollection = routes.Build()
app.UseRouter(routeCollection);
Attributes
public sealed class HiController
{
// GET: /hi
public IActionResult Get() => Ok("Hello");
}
public sealed class TestController : Controller
{
// /hello
[Route("/hello")]
public IActionResult Hello()
=> Ok("Hello");
// GET: /hi
[Route("/hi")]
[HttpGet]
public IActionResult Hi()
=> Ok("Hi");
// GET: /hi
[HttpGet("/hi")]
public IActionResult Hi()
=> Ok("Hi");
}
//Route prefix
[Route("test")]
public sealed class TestController : Controller
{
//You can have multiple routes on an action
[Route("")] // /test
[Route("hello")] // /test/hello
public IActionResult Hello()
=> Ok("Hello");
// Maps to both:
// /test/hi, and:
// /hi
[Route("/hi")] // Overrides the prefix with /, you can also use ~/
[Route("hi")]
public IActionResult Hi()
=> Ok("Hi");
// /test/greet/Joonas -> maps Joonas to the name parameter
[Route("greet/{name}")]
public IActionResult Greet(string name)
=> Ok($"Hello {name}!");
//Parameters can be optional
// /test/greetopt -> name == null
// /test/greetopt/Joonas -> name == Joonas
[Route("greetopt/{name?}")]
public IActionResult GreetOptional(string name)
=> Ok(name == null ? "No name" : "Hi!");
}
// You can use [controller], [action], and [area] to create generic templates
[Route("[controller]/[action]")]
public sealed class MyController : Controller
{
// /my/info
public IActionResult Info()
=> Ok("Info");
// /my/i
[Route("/[controller]/i")]
public IActionResult Info2()
=> Ok("Info2");
}
[Route("user")]
public sealed class SelectionController : Controller
{
//You can use constraints to influence route selection
//Do not use for validation!
// /user/123
[Route("{id:int}")]
public IActionResult Int(int id)
=> Ok($"Looked up user id {id}");
// /user/foo
[Route("{name:alpha}")]
public IActionResult String(string name)
=> Ok($"User name {name}");
}
Quellen
- Ryan Nowak, Rick Anderson: Routing to Controller Actions. In: Microsoft Docs, ASP.NET Core. Microsoft, 14. März 2017, abgerufen am 12. Mai 2017 (englisch).
- Joonas Westlin: Attribute routing cheat sheet for ASP.NET Core. 22. Oktober 2016, abgerufen am 12. Mai 2017 (englisch).
|}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.