Code Monkey home page Code Monkey logo

Comments (10)

georgiosd avatar georgiosd commented on September 18, 2024

This seems impossible based on my findings here: https://aspnetwebstack.codeplex.com/discussions/451219

Any ideas?

from regexrouting.

heymagurany avatar heymagurany commented on September 18, 2024

Good point. It looks like this will have to wait until the next version of ASP.NET. In the meantime, I've been using System.Web.Routing.RouteCollection to map routes, for which there is an extension method available in the Magurany.Web.Mvc.RegularExpressions namespace

from regexrouting.

georgiosd avatar georgiosd commented on September 18, 2024

You mean API routes? Can you give an example?

from regexrouting.

heymagurany avatar heymagurany commented on September 18, 2024

After installing both the Magurany.Web.Routing.RegularExpressions and Magurany.Web.Mvc.RegularExpressions NuGet packages, you can add the following code to your API route registration:

using Magurany.Web.Routing.RegularExpressions;
...
protected void Application_Start()
{
    routes.MapRegexRoute(
        "RegexRoute",
        "{controller}",
        @"^/(?<controller>segment)/\d+$",
        new { controller = "home", id = RouteParameter.Optional },
        HttpControllerRouteHandler.Instance);

    GlobalConfiguration.Configuration.MessageHandlers.Add(new RegexRouteHandler(config));
}
...

That's probably a weird example, but the thing to take away from it is the last parameter of MapRegexRoute. That's how I get requests to dispatch to the Web API runtime.

I did run into a bug, however. Custom routes aren't aren't accounted for by Web API's default message handler, which casts routes to System.Web.Http.WebHost.Routing.HttpWebRoute. So I had to install a custom message handler to take care of this problem. That is what you're seeing on the last line of the example. That class is not included in the NuGet package (because that's part of my Web API support work), but it is in the source code here: /src/RegexRouting/RegexRouteHandler.cs

from regexrouting.

georgiosd avatar georgiosd commented on September 18, 2024

Funny, I concluded to the same solution: http://stackoverflow.com/questions/17856926/how-to-modify-asp-net-web-api-route-matching-to-allow-parameters-with-slashes-in - feel free to add your answer there to give more visibility to your project.

from regexrouting.

georgiosd avatar georgiosd commented on September 18, 2024

Hm... this is making my API tests (self-hosted Web API) fail (I get a 404 on the routes!) - the culprit seems to be the RouteHandler. Any ideas how to resolve this?

from regexrouting.

heymagurany avatar heymagurany commented on September 18, 2024

I've never testing this in a self-hosted environment before. I'll setup a test and let you know my findings. Do you know if the IHttpRoute interface is used correctly by the web API runtime in a self hosted environment?

from regexrouting.

georgiosd avatar georgiosd commented on September 18, 2024

So here's the drill:

  • IHttpRoute implementations work just fine when self-hosted. Seems like Routing does not rely on System.Web.Routing or something
  • I made it work on both environments by checking whether it's self-hosted and then either adding regex routes as native IHttpRoutes or converting them to Routes if it's web-hosted, remembering to clear the RouteTable first (important when running tests because the RouteTable is statically initialized)

The only thing I haven't managed to figure out is how to check for self-hosted without having a reference to the self-host nuget, which is really not necessary. I guess I could do something hacky like get the config type name and check for the self-hosted config type name... but that's hacky...

from regexrouting.

heymagurany avatar heymagurany commented on September 18, 2024

Were those changes you made to the code from this repo? If so, I think that's worth pulling in for self-hosting support.

I agree that the config type check is a bit gross. When you're adding the route, are you adding it to a RouteCollection or an HttpRouteCollection. That might be the differentiator since we know that's not going to work when hosted in IIS. Of course, that won't be the case when the next version does support IHttpRoute.

from regexrouting.

georgiosd avatar georgiosd commented on September 18, 2024

No, not on this repo, I would have pulled it already :) I had already started working on this before you answered so I kinda went my own way. But I'm happy to give any guidance you may need.

You mean in the self-host scenario? I am adding directly to HttpRouteCollection as it should be. It's all working well there.

from regexrouting.

Related Issues (2)

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.