I'm having some trouble writing custom routing for an MVC application that I'm developing.
I've created a class in the App_Start folder called Startup that looks like this:
public class Startup : IApplicationEventHandler
{
public void OnApplicationStarted(UmbracoApplicationBase application, ApplicationContext context)
{
}
public void OnApplicationStarting(UmbracoApplicationBase application, ApplicationContext context)
{
RouteTable.Routes.MapRoute("GetCart", "Umbraco/Api/Cart", new { controller = "Cart", action = "Index" });
}
public void OnApplicationInitialized(UmbracoApplicationBase application, ApplicationContext context)
{
}
}
When I then visit the url: "site.com/Umbraco/Api/Cart" I get a blank page.
But if I visit the url: "site.com/Umbraco/Api/Cart/Index" that the custom route should map to, I'm displayed the content that I want.
Am I doing something wrong here?
My CartController looks like this:
public class CartController : UmbracoApiController
{
[HttpGet]
[ActionName("Index")]
public string GetCart()
{
return "";
}
[HttpGet]
[ActionName("Mini")]
public string GetMiniCart()
{
return "";
}
[HttpPost]
[ActionName("Products")]
public string ProductsAdd()
{
return "";
}
[HttpPut]
[ActionName("Products")]
public string ProductsUpdate(int productId)
{
return "";
}
[HttpDelete]
[ActionName("Products")]
public string ProductsDelete(int productId)
{
return "";
}
}
Well, what I'm trying to do is have my urls make sense.
Problem is, I have two CartControllers actually, one is an ApiController and one is a SurfaceController.
I do not want my link to the ApiController to look like this: site.com/Umbraco/Api/CartApi - That doesnt't make sense. Instead I want it to be: site.com/Umbraco/Api/Cart. That is why I'm doing the custom routing.
Problem being that, if I remove my custom routing I get a 404 trying the access the url: site.com/Umbraco/Api/Cart.
Looking at your code your route (without custom routing) would be /umbraco/api/cart/index
But if you don't want to add the index method the only ways is custom routing, but then you wouldn't need to inherit from UmbracoApiController, but the default API controller
Sadly, inheritting from the default ApiController did not help anything :-\
I still get a blank page when accessing site.com/Umbraco/Api/Cart but now I simply can't access site.com/Umbraco/Api/Cart/Index of course, because I removed the routing automatically done by Umbraco.
No change at all. I'm still just getting a blank page.
Weird thing is that I don't get a 404. And if I set breakpoints, I don't hit the method in my controller.
Problem with custom routing in Umbraco 7
Hello,
I'm having some trouble writing custom routing for an MVC application that I'm developing.
I've created a class in the App_Start folder called Startup that looks like this:
When I then visit the url: "site.com/Umbraco/Api/Cart" I get a blank page. But if I visit the url: "site.com/Umbraco/Api/Cart/Index" that the custom route should map to, I'm displayed the content that I want.
Am I doing something wrong here?
My CartController looks like this:
First of all. You don't need to setup routing for UmbracoApiControllers. This handled by the framework.
You can find some more documentation here :
http://our.umbraco.org/documentation/Reference/WebApi/
http://our.umbraco.org/documentation/Reference/WebApi/routing
Especially the last link will be useful for you.
Dave
Well, what I'm trying to do is have my urls make sense.
Problem is, I have two CartControllers actually, one is an ApiController and one is a SurfaceController.
I do not want my link to the ApiController to look like this: site.com/Umbraco/Api/CartApi - That doesnt't make sense. Instead I want it to be: site.com/Umbraco/Api/Cart. That is why I'm doing the custom routing.
Problem being that, if I remove my custom routing I get a 404 trying the access the url: site.com/Umbraco/Api/Cart.
Looking at your code your route (without custom routing) would be /umbraco/api/cart/index
But if you don't want to add the index method the only ways is custom routing, but then you wouldn't need to inherit from UmbracoApiController, but the default API controller
Dave
Sadly, inheritting from the default ApiController did not help anything :-\
I still get a blank page when accessing site.com/Umbraco/Api/Cart but now I simply can't access site.com/Umbraco/Api/Cart/Index of course, because I removed the routing automatically done by Umbraco.
If you inherit from API controller you should route your request :
Dave
No change at all. I'm still just getting a blank page.
Weird thing is that I don't get a 404. And if I set breakpoints, I don't hit the method in my controller.
And if you make action an optional url parameter ?
Can I ask why you want your API url's like this? Is it going to be available to 3rd parties ?
Dave
If I set action to UrlParameter.Optional I get the following error:
routedata must contain an item named 'action' with a non-empty string value
And you can absolutely ask why.
Yes, it's part of a webshop, so we would like to make the url as "pretty" as possible :-)
Problem solved.
To map the route correctly you will have to use RouteTable.Routes.MapHttpRoute instead of RouteTable.Routes.MapRoute and it will work perfectly.
is working on a reply...