Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing #61

Closed
danroth27 opened this issue Apr 16, 2015 · 14 comments
Closed

Routing #61

danroth27 opened this issue Apr 16, 2015 · 14 comments
Assignees
Labels
Pri0 Urgent priority
Milestone

Comments

@danroth27
Copy link
Member

ASP.NET 5->Fundamentals->Routing

  • Middleware
    • Maps a request to an IRouteHandler
    • The IRouteHandler returns a RequestDelegate or null
      • Example: is there a matching MVC action
    • If null, then routing continues
    • If no route handler can handle the request then the middleware calls next
  • Configuration
    • Create RouteBuilder
  • Code sample to reference: https://github.com/aspnet/Routing/blob/master/samples/RoutingSample.Web/Startup.cs
  • Template routes
    • Ex: {controller=Home}/{action=Index}/{id?}
    • Stuff with curly braces defines a route value that will get bound
    • Stuff with an = defines a default value. If the path segment is not present then the default value is added to the route values
    • ? defines an optional value
    • : to add inline constraints
    • Prefix route parameter with * to bind to the rest of the URI. Ex /blog/{*slug}
    • Route templates need to be unambiguous. Ex this doesn't work: {id?}/{foo}
    • Complex segments can contain text and parameters. Ex {mm}-{dd}-{yyyy}
      • Special complex segment can have an optional file extension (i.e. the dot is special because it is optional too). Ex /files/{filename}.{ext?}
  • MapRoute extension methods
  • Default values
    • Get merged with the defaults in the template, but conflicts cause errors
  • Data tokens
    • User defined data bag that gets carried along if the route matches
  • Link generation
    • Give routing a set of route values and optionally a route name to produce a URL
    • Current route values of the current request are considered ambient values for link generation
      • Ex When on the About action of the Home controller to link to the Index page you don't need to specify the controller route value.
    • Ambient values that don't match a parameter are ignored
    • Ambient values are ignored when a value explicitly provided overrides it going from left to right in the URL
    • Values the are explicitly provided that don't match anything go into the query string
    • If a route has default values that don't match a parameter and that value is explicitly provided it must match the default value.
@Daniel15
Copy link
Contributor

Daniel15 commented Aug 1, 2015

I know this is probably the wrong place to ask, but does ASP.NET 5 support attribute routing (ie. defining routes as attributes on the controllers/actions rather than in one giant config file)?

@davidfowl
Copy link
Member

yes, MVC 6 does

@Daniel15
Copy link
Contributor

Daniel15 commented Aug 1, 2015

Oh cool, thanks! I'm currently using MVC 4, I didn't realise that attribute routing is actually built into the framework starting from MVC 5 (and MVC 6 has the same attributes)

@danroth27
Copy link
Member Author

We should probably also mention common conflicts with routing and IIS:

  • Routes with dots generally have issues with static file handling
  • Routes with "unsafe" characters get rejected by IIS

@rmcc13
Copy link

rmcc13 commented Oct 17, 2015

This may be the wrong place to ask, but would a third party person pick up an issue such as this one to work on it? The contributing guidelines say to open up a new issue, but there are many open issues that I would be interested in working on. Should I just work on one then submit a pr?

@Tessalator
Copy link

@danroth27 by "Routes with dots" you are talking about route templates correct? Or does this apply to route names too?

Also, I'd like to contribute to some type of migration documentation. I'm working on moving an MVC5 application to MVC6 and hit some major roadblocks due to discontinued features. As I find the way to implement my features in 6 I'd like to share those with others. Where would I contribute these things?

@Daniel15
Copy link
Contributor

Daniel15 commented Dec 5, 2015

by "Routes with dots" you are talking about route templates correct?

Yeah, route templates, things like these ones I use on my site:

[Route("[action].htm")]
[Route("category/{slug}.rss", Order = 2, Name = "BlogCategoryFeed")]

@kylone
Copy link

kylone commented Dec 8, 2015

One of the issues I've had problems with ASP.NET MVC (4+) and IIS is handling making custom Not Found and Error pages is all cases. We eventually went with web server directives. Would a middleware piece be appropriate for this?

@danroth27 danroth27 added this to the 1.0.0-rc2 milestone Dec 15, 2015
@ardalis ardalis self-assigned this Jan 16, 2016
@ardalis
Copy link
Contributor

ardalis commented Feb 12, 2016

All these links are to AspNetCore / RC2 files; should I target RC2 with this article or ? @danroth27 @rynowak

@danroth27
Copy link
Member Author

Target RC1. I moved the post-RC1 stuff to a separate issue: #986

@danroth27 danroth27 added the Pri0 Urgent priority label Feb 16, 2016
@ardalis
Copy link
Contributor

ardalis commented Feb 21, 2016

@rynowak what is the purpose of DefaultHandler and ServiceProvider on RouteBuilder? They don't seem to be used anywhere except the sample project.

@ardalis
Copy link
Contributor

ardalis commented Feb 21, 2016

Nevermind, it seems to be necessary for extension methods to work, such as MapRoute()

@ardalis
Copy link
Contributor

ardalis commented Mar 7, 2016

@rynowak From our discussion (notes above): "If a route has default values that don't match a parameter and that value is explicitly provided it must match the default value." This relates to link generation.
I'm not sure how to create an example for this. Here's a route with default values:
"{controller=Home}/{action}"
I'm not sure what you meant by "that don't match a parameter" Now if I go to generate a link for this route, and I pass in an explicit value of "controller=Home", it should do what I expect. If I pass in an explicit value of "controller=Products" it should still do what I expect (generate /Products/something). When would the rule you're describing come into play?

thanks

@ardalis ardalis mentioned this issue Mar 7, 2016
@rynowak
Copy link
Member

rynowak commented Mar 7, 2016

Here's the example:

routes.MapRoute("blog_route", "blog/{*slug}", defaults: new { controller = "Blog", action = "ReadPost" });

This will only generate a link when the matching values for controller and action are provided.

trobinson41 pushed a commit to trobinson41/Docs that referenced this issue May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri0 Urgent priority
Projects
None yet
Development

No branches or pull requests

9 participants