rwkiii
rwkiii

Reputation: 5846

How to control or change the URI to MVC4 Views

I have an MVC4 Web API Application. I placed all of the APIController classes under Controllers/API and now I am creating MVC Controller classes in the Controllers/ folder.

I Add a controller named UserController specifying Read/Write with views using EntityFramework. This automatically generates a folder under /Views called Users. It contains the Create.cshtml, Delete.cshtml, Index.cshtml, etc. files.

Problem is that I want the URI of this User view to be /Admin/User/

Did I miss an opportunity to specify creating the view under the Admin/ folder? I tried copying the entire folder where I wanted it, but it isn't accessible that way. I delete the controller and view, then recreated the controller but I didn't see anyplace I could set that would cause the view to get created under Admin/.

How do I create the MVC controller, and autogenerate the CRUD pages under a specific Views folder?

-- UPDATE --

I added the route for Admin/. Here is my RouteConfig.cs:

public class RouteConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Administration",
            routeTemplate: "Admin/{controller}/{action}"
        ); 
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

I don't know if it matters, but I have a MVC controller for Admin - it's called AdminController. Thinking it might be in the way, I changed routeTemplate: "Admin/{controller}/{action}" to routeTemplate: "Admin1/{controller}/{action}", but it made no difference.

Here is the error I am seeing:

<!--  
[HttpException]: A public action method &#39;user&#39; was not found on controller &#39;MYPROJECT.Web.API.Controllers.AdminController&#39;.
   at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
 -->

After the CRUD pages are automatically generated there is a new folder created with the .cshtml files inside it. Except, it is created at /Views/User/. Is it ok that I move (drag/drop in VS) this /User/ folder so that it is located at /Views/Admin/User/? (If for no other reason but for organization).

Upvotes: 1

Views: 624

Answers (1)

Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

If You want to change the URI You should register new route. Go to WebApiConfig.cs in App_Start folder. Here You register routes for ApiControllers. Add the following route above the default route:

config.Routes.MapHttpRoute(
    name: "SpecifyYourRoutName", // optional
    routeTemplate: "Admin/User/{action}",
    defaults: new { controller = "User" }
);

Now when You access Admin/User/Create You will hit Create() method of User controller.
If You want to pass some data in the URI like for instance id of the item to be deleted You just write: Admin/User/Delete?1 and this will call Delete(int id) method of User controller and pass 1 as id to this method.

If You will have other controllers with similar URIs You can define Your route like this:

config.Routes.MapHttpRoute(
    name: "SpecifyYourRoutName", // optional
    routeTemplate: "Admin/{controller}/{action}"
);

In this case You can query URIs like Admin/OtherControllerName/ActionOfThisController?param1=something1&param2=something2...

Upvotes: 1

Related Questions