ASP.NET Web Api Controller subfolder routing - asp.net

I’m going crazy here… this seems like a very simple task. First off, I know only the basics of the Web Api and MVC – so please don’t skewer me.
In the project I need to logically create controller subfolders (for organization purposes). I had a feeling it wasn’t as simple as I thought. I have the default route like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Which works as it should directly from the controllers folder in my project. I have added a subfolder in the controllers folder controllers/reports. I have searched quite a bit and just can't quite find a solution. How can I add a route that will direct to the subfolder.
I have tried:
config.Routes.MapHttpRoute(
name: "ReportingApi",
routeTemplate: "api/Reports/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
and:
config.Routes.MapHttpRoute(
name: "ReportingApi",
routeTemplate: "api/Reports/{id}",
defaults: new { controller = "userunit" id = RouteParameter.Optional }

Nevermind I'm an idiot... I left the default route in, removed the "Reports" in the url. It found the controller even though it was in a subfolder.

Related

How to redirect the main URL to new path in ASP.NET MVC

I am using ASP.NET MVC, In localhost, using the web, I changed the start URL option it works fine there, once I published it to the main domain, it is not working on that. I hope anyone can help me. I want to redirect from
domain.com/
to
domain.com/Home/Index
Thanks a lot!
you need to change your default route as this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
on startup the default route is initialized and hence redirected to the above website as
domain.com/Home/Index

the resource cannot be found asp.net mvc

hi i already tried some solution available on stack overflow...some of them are configuring in routeconfig, going to project properties and creating a virtual directory. But none of them work for me. I've been working on this project and i don't know what happened suddenly. I've tried restarting laptop as well as visual studio . Please help it was working fine before
UPDATE:i have 2 controllers that are not working properly admin and account controller both have views as well as their corresponding controller
Remove all your existing routes and just use the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This should keep things simple and should also work with your existing controller/actions.
I think the issue is to do with your routes. Can you try an explicit route like the one below and check that it works?
routes.MapRoute(
name: "AdminIndex",
url: "admin/index",
defaults: new { controller = "Admin", action = "Index" }
);
Can you try adjusting your admin route like so:
routes.MapRoute(
"Admin",
"{controller}/{action}/{id}",
new { action = "Index", controller = "Admin", id = UrlParameter.Optional }
);

ASP.NET Core MVC route to controller any child url like "admin/*"

I want to route any url address like /admin/* as my admin is on Angular2 and uses its routing. I tried template: "admin/{*url}"though it works not only with /admin url but with any other. Is there any way to solve this problem?
Add this code in your WebApiConfig.cs file :
config.Routes.MapHttpRoute(
name: "some name",
routeTemplate: "admin/{*url}",
defaults: new { controller = "Admin", action = "Your default action here" });
Set the default controller in defaults.

How to Routes Web API controller Existing Web Form Project

I tried implement web API controller for my existing web form project, i used following code for route my API.
RouteTable.Routes.MapHttpRoute(
name: "EnadocApi",
routeTemplate: "apiv2/{controller}/{action}/{Id}",
defaults: new { Id = RouteParameter.Optional }
);
But it gave following error.
Error
I used VS2015.
The first thing I'd try is to add a reference to System.Web.Http, then add that as a using in your RouteConfig class.
using System.Web.Http;

ASP.Net MVC No routes but default are working

I have added a new Controller to my application, and then from there added in a new View (right click, add view on the context menu)
I have added the route to the Route table, but when I browse to it I get a 404.
I can only assue that this is something fundamental that is wrong, as if I replace the values in the default route, I still cannot access my page.
The route table is;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Classroom",
"{controller}/{action}/",
new { controller = "Classroom", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//defaults: new { controller = "Classroom", action = "Index", id = UrlParameter.Optional }
);
there is only a return View(); in the index method. I have installed Glimpse, but as I am getting a 404, I cannot actually see the url that is being passed back.
So, what techniques / tricks are there I can use to track this down, (and any common gotcha's that could be causing this?)
Visual Studio 2012, using the inbuilt webserver.
This is Fixed. It was a stupid Gotcha on my part. The Controller was Called Classroom not ClassroomController
I have added the route to the Route table
No, you don't need to add any route for that. Get rid of the Classroom route from your Global.asax and leave only the Default one. Now you could safely navigate to your controller using /classroom/index which will render the Index action of the ClassroomController.

Resources