Developer forum

Forum » Development » Defined routes not working

Defined routes not working

Lars Larsen
Lars Larsen
Reply

Hi

I am using a component that needs some routes in order to work. I use the compontent on a Dynamicweb v9.9.1. I have defined the routes in the notification subscriber "Standard.Application.BeforeStart". But I can't make the routes work. Does Dynamicweb ignore routes? If so how can I make my routes work?


Replies

 
Nicolai Pedersen
Reply

Thanks for showing your code... :-).

Take a look at this thread - and if that does not help, show us your setup.

Thanks, Nicolai

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

Thanks for showing me the link wink (it's missing)

BR
Lars

 
Nicolai Pedersen
Reply

Ha, laugh Sorry!!

Here you go: https://doc.dynamicweb.com/forum/development/development/mvc-duplicate-routes

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

I have seen the thread you link to and your webinar "Setting up MVC / web api on Dynamicweb 9" and followed the guidelines from there. But in this line:

System.Web.Routing.RouteData routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(new System.Web.HttpContextWrapper(myargs.Application.Context));

I get an error for myargs.Application.Context, saying:

'IApplication' does not contain a definition for 'Context' and no accessible extension method 'Context' accepting a first argument of type 'IApplication' could be found (are you missing a using directive or an assembly reference?)

 

 
Lars Larsen
Lars Larsen
Reply

Bump!

 
Martin Vang
Martin Vang
Reply

Hi Lars,

We had to remove alot of our direct usage of things like HttpApplication in our push towards .Net Core, so this is one of those things that had to change.

Luckily you can fairly easily access the correct information, but instead of using the property from myArgs, you can access HttpContext.Current.Application.Context (or just use HttpContext.Current ?). Anyway, try it out and give me an update on the progress?

I don't know exactly what kind of routes you're trying to add for the component (or why), but if it's to register webapi endpoints with corresponding methods that return values, it's probably easier to do something like this:

using System.Web.Http;

[RoutePrefix("api/products")]
    public class ProductsController : ApiController
    {
        [HttpGet, Route("")] // api/products

        public IHttpActionResult GetAll() { return Ok([valueThatShouldBeReturned])}//note that this also handles things like status-code (here it's 200, but you can easily return an error-status if you catch an exception)

        [HttpGet, Route("{IdName}")]// api/products/valueOfMyId

        public IHttpActionResult GetById(string idName) { [...]} //note that the parameter name should match the literal in the Route attribute

}

This way it's easy to read what method belongs to a specific route and what type it is.

BR

Martin

 
Lars Larsen
Lars Larsen
Reply

Hi Martin

I couldn't make it work. So I went with an other solution. Thanks anyway.

 

You must be logged in to post in the forum