GithubHelp home page GithubHelp logo

aspnet / routing Goto Github PK

View Code? Open in Web Editor NEW
272.0 82.0 126.0 6.86 MB

[Archived] Middleware for routing requests to application logic. Project moved to https://github.com/aspnet/AspNetCore

License: Apache License 2.0

Shell 0.18% C# 99.67% Batchfile 0.01% PowerShell 0.13%
aspnet-product

routing's Introduction

ASP.NET Routing [Archived]

This GitHub project has been archived. Ongoing development on this project can be found in https://github.com/aspnet/AspNetCore.

Contains routing middleware for routing requests to application logic.

This project is part of ASP.NET Core. You can find samples, documentation and getting started instructions for ASP.NET Core at the AspNetCore repo.

routing's People

Contributors

ajaybhargavb avatar anpete avatar aspnetci avatar brennanconroy avatar bricelam avatar chengtian avatar danroth27 avatar davidfowl avatar dotnet-maestro-bot avatar dougbu avatar drieseng avatar eilon avatar haok avatar harshgmsft avatar ianhong avatar jamesnk avatar javiercn avatar jbagga avatar juntaoluo avatar kichalla avatar loudej avatar mnltejaswini avatar natemcmaster avatar ntaylormullen avatar pakrym avatar pranavkm avatar ryanbrandenburg avatar rynowak avatar tratcher avatar troydai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

routing's Issues

RouteCollection should be threadsafe

I am working on an application where I need to add the routes dynamically to the RouteCollection. But current RouteCollection is not designed for this. It is not thread safe. I need to implement another RouteCollection with the exact same functionality that is thread safe.

Can we make current RouteCollection thread safe so we can add routes dynamically?

Should GetVirtualPath return PathString

Inside URL generation in MVC we need to do some stuff that PathString already handles. Would be easier if GetVirtualPath just returned PathString.

We have to harden this code against a missing leading slash because 3rd party route code could do it. Using PathString would enforce this.

            var context = new VirtualPathContext(_httpContext, _ambientValues, values);

            var path = _router.GetVirtualPath(context);

            PathString pathString;
            if (path.Length > 0 && !path.StartsWith("/", StringComparison.Ordinal))
            {
                pathString = new PathString("/" + path);
            }
            else
            {
                pathString = new PathString(path);
            }

            return _httpContext.Request.PathBase.Add(pathString).Value;

Add Datatokens Support

Couldn't find a bug for this

DataTokens needs to be added to RouteData as a Dictionary<string,object>. TemplateRoute (and MapRoute(...)) need to accept data tokens from the user. When the route matches, the values should be copied to the RouteData.DataTokens

Also consider here making it IReadOnlyDictionary<string, object>

Route DataTokens- Readonly

Lifting the comment from #88
This might allow us to do re-use the Dictionary when the passed in value is null. I'm not familiar with the use case for this, but is it meant to be modifiable by a user?

RouteTemplate does not take RouteData

Consider the following scenario:

    public class CountryRoute : TemplateRoute
    {
        public CountryRoute(IRouter target, string routeName, string routeTemplate, 
            IDictionary<string, object> defaults, IDictionary<string, object> constraints, 
            IInlineConstraintResolver inlineConstraintResolver) : 
            base(target, routeName, routeTemplate, defaults, constraints,  inlineConstraintResolver)
        {
        }

        public override Task RouteAsync(RouteContext context)
        {
            var host = context.HttpContext.Request.Host;

            var split = host.Value.Split(',');

            string countryCode = null;
            if (split.Length > 3)
            {
                countryCode = split[0];
            }

            context.RouteData.Values["Country"] = split;
            return base.RouteAsync(context);
        }
    }

In MVC5 this would produce routevalues in this implementation.

See:
https://github.com/aspnet/Routing/blob/dev/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs#L111

Implement a 'not allowed' constraint

This is a companion to the 'required' constraint. This is a companion to #79 that can fixup some special cases with link generation when one route has fewer tokens than another.

Ex:
{controller}/{action}
api/{controller}

Imagine an MVC controller with both an Index and Get (unnamed) action. If you try to generate a link @Url.Action("Edit", "Home") and there is no Edit action, then the second route will generate the url api/Home?action=Edit.

This is technically valid, but it's not what you intended when you wrote these routes. Having a constraint like { action = new NotAllowedRouteConstraint() } on the second route will prevent this from happening.

Query string binding incorrect for array of enums

Use the following code to repro:

    [Flags]
    public enum HttpVerbs
    {
        None = 0,
        Get = 1 << 1,
        Post = 1 << 2,
        Put = 1 << 3
    }

    public class SimulatedRouteConfigQueryStringModel
    {
        public HttpVerbs[] Verbs { get; set; }
    }

    // Place the following method in a controller:
    public string Config(SimulatedRouteConfigQueryStringModel model)
    {
        return string.Join("|", model.Verbs);
    }

Do an HTTP Get to the config method and pass the following parameters:

<URL>/Config?Verbs=Get&Verbs=Post

Expected: the method will return "Get|Post"
Actual: the method returns "None|None"

Convention based routing

Hi,

Would it be possible to introduce convention based routing? If not thne would it be possible to introduce an option that is based on namespaces? e.g. Controllers\Admin\UserManagement\DeactivationsController.PendingActions would map to Admin\UserManagement\Deactivations\PendingActions url. This option would need to support any number of subnamespaces so it is not limited in the way areas are.

Thanks

Pawel

Route tables should be unit testable

Has any thought been given to testing route tables? This is not addressed in MVC5, so you get issues where the necessary methods are internal, undocumented and change frequently, like this

Data Tokens support

Add data tokens and flow as part of our context objects

| 196 | 10410 | Reported By: Ryan Nowak |

Design passing top level router to applications

We need a way to provide the top level IRouter instance to applications. Using service provider isn't great because an application could be reachable via multiple routes.

SetFeature isn't great because we'd need to remove it if not handled. The RouteContext might be the best way.

RouterMiddleware should resolve ILoggerFactory from DI in the constructor

RouterMiddleware currently resolves the ILoggerFactory on the first request from RequestServices.
https://github.com/aspnet/Routing/blob/dev/src/Microsoft.AspNet.Routing/RouterMiddleware.cs#L61

Instead, it should resolve it as a DI constructor parameter. See CookieAuthMiddleware for example:
https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs
https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationExtensions.cs

Also, RequestServices isn't guaranteed to be populated:
aspnet/Security#72

Create a new routedata for each 'router'

For each hop in the graph of routers, we want to create a new copy of route data, so that each stage can write to it without dirtying data if it fails to handle the request.

Also, RouteData.Routers should be a logical stack of IRouter instances that represent the current execution.

Inside an MVC action in the default case this will look like:
[0]: Route Collection
[1]: Template Route
[2]: MVC Route Handler

Consider IDictionary<string, string> for route values

@lodejard feedback

Currently it's IDictionary<string, object> - this leads to us writing a fair bit of code to stringify values, and it's different code in some places, likely not intentionally.

There would be real difficulties for anyone implementing a system where route values are non-strings, and they would encounter bugs if their values don't convert to strings in an orderly way.

Routing does not encode / in segments

If you have "{controller}/{action}" and new { controller = "cool/beans", action = "index" } you get "cool/beans/index". This doesn't roundtrip via parsing, so it's almost certainly something we want to change.The System.Web implementation does not deal with this case.

From @Eilon

I suspect that this might technically be a long-running bug in routing as well. I think technically we want to encode each URL segment and then combine those together with literal slashes. It is logically incorrect to "encode" or "decode" a URL because by definition a URL is always encoded. Only segments of a URL or query string can be encoded or decoded.

An example of something that I think is broken is if I have a URL segment that's supposed to have a slash in it, such as blog/tags/{tagNames}, and the tagNames segment can have arbitrary text in, including slashes in the form of %2f. So the incoming URL is like /blog/tags/slice%2fdice. Assuming IIS or someone didn't already ruin the URL for us (which they might have...), we'll get tagNames="slice/dice" - that's great. But if we tried to generate the URL you'd get /blog/tags/slice/dice, which is incorrect. Hopefully this made a tiny bit of sense ๐Ÿ˜„

Enable DI for use at startup in Routing.

While parsing the template Routing needs access to the following:

  1. IInlineConstraintResolver
  2. IRouteConstraints should be provided to the IInlineConstraintResolver using DI.
    Refer Pr #50

Consider not running constraints on optional parameters/default values

In the following routing registration code:

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: new { id = "a.*" }
            );

or

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = null },
                constraints: new { id = "a.*" }
            );

The user was expecting that for the request url /home/index the route will match, but it doesn't because of the constraints, and asked if we can avoid running the constraint if the value is the default value. Otherwise a special regex must be written.

The danger here is that if we do that, custom constraints that might have nothing to do with the actual values will not run, so it's seems like it will work in many cases, but will block custom constraints scenarios.

From:
https://aspnetwebstack.codeplex.com/workitem/1552

Resolve API versioning with new routing core

With 5.x series and introduction of route constraints api versioning became possible, but we still lack lots of functionality when it comes to generating documentation and treating the version as core concept of the framework.

I suggest to take a look from a very beginning making sure the versioning will become a core concept in the routing before we start investing into writing self documenting features for the apis.

app.UseRouter() extension should return IBuilder

Moved from aspnet/Mvc#134

app.UseRouter() should return an IBuilder instead of IRouteCollection to support chaining of middlewares like this:

app
.UseFileServer()
.UseRouter() //<-- Currently this is not possible as this returns an IRouteCollection
.MapSignalR()

Routes need to be able to filter on the presence of a value

With routes like this:

routes.MapRoute("{controller}",  new { controller = "Home" });
routes.MapRoute( "{controller}/{action}",  new { controller = "Home", action = "Index" });

And data like

{ controller = "Home", action = "Details" }

The REST route will be able to create a URL - which isn't desirable.
?action=Details

We could solve this problem with constraints, but we might want a better way.

Make ambient values part of 'accepted values' during the constraints part of link generation.

We accidentally changed this behavior from old MVC. This lets us do some pretty useful stuff with constraints so we should bring it back.

Here's an example (old MVC):

routes.MapRoute(
                name: "Default_slug",
                url: "slug/{controller}/{action}",
                defaults: new { controller = "Home", action = "Index", foo = "bar" },
                constraints: new { foo = new MyConstraint() });

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: new { foo = new MyConstraint() });

If you navigate to / then the second route will match, and produces the ambient values:
{ controller = "Home", action = "Index" }.

Then inside that action, generate a link to another action Url.Action("About").

The inputs to link generation:
values: { action = "About" }
ambient values: { controller = "Home", action = "Index" }

When constraints are called, they see the 'accepted values:
{ controller = "Home", action = "About" }

Note that the value foo = "bar" is not present, even when running the first route's constraints.

Now repeat, but navigate to /slug to match the first route, producing the ambient values:
{ controller = "Home", action = "Index", foo = "bar" }.

Generate the same link:
values: { action = "About" }
ambient values: { controller = "Home", action = "Index", foo = "bar" }

When constraints are called, they see the 'accepted values:
{ controller = "Home", action = "About", foo = "bar" }

Note that foo = "bar" is present because it's an ambient value and not because it's a default value of the route.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.