MVC5 Custom Route to Remove Controller Name from Area

clockwiseq

So far I can't make this work and I need some assistance please. I have a basic MVC 5 site, and I have added an area called Administration. For the life of me, I can't figure out how to set a default controller/action for an area, properly.

In my site, I have an area named Admin, a controller named Admin (with Index method and view), and this is the area registration:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        name: "Admin_base",
        url: "Admin",
        defaults: new { area = "Admin", controller = "Admin", action = "Index", id = UrlParameter.Optional }
    );

    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

The first MapRoute allows me to browse to http://myapplication/Admin and it displays a view that I set (Admin/Index) and the URL stays http://myapplication/Admin (which is what I want). Now, by adding this, it breaks any further routing to a controller. So when I attempt to navigate to the Menu controller in the Admin area, it fails.

Is there a correct way to do this?

i.e. I need to make http://myapplication/Admin/Menu/Create route properly, but also need to retain the default Controller/Action for the area.

Erik Philips

You should just be able to combine them into one:

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    defaults: new { area = "Admin", 
                    controller = "Admin", 
                    action = "Index", 
                    id = UrlParameter.Optional }
);

By assigning defaults, you should be able to call /Admin/ and the rest of the parameters are set to the defaults.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Calling a controller action MVC5

来自分类Dev

MVC5 Html.RenderAction with different controller

来自分类Dev

Custom HTML Helpers in MVC5 with Model Lambda

来自分类Dev

User.Identity.Name全名mvc5

来自分类Dev

User.Identity.Name全名mvc5

来自分类Dev

Rails Call Custom Controller Action from Custom Form

来自分类Dev

Laravel 5中route :: controller上的参数命名

来自分类Dev

MVC5属性路由等效于route.config路由

来自分类Dev

How to call a route by its name from inside a handler?

来自分类Dev

从View将项目添加到List并传递到MVC5中的Controller

来自分类Dev

使用ActionResult访问Action / Controller的MVC5被跳过,并产生“找不到资源”错误

来自分类Dev

如何将DateTime参数从View传递到ASP.net MVC5中的Controller?

来自分类Dev

需要 Laravel Route::controller 和 Route::controllers

来自分类Dev

New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

来自分类Dev

Pass object from Javascript to MVC Controller

来自分类Dev

MVC5:通过Ajax发布JSON无法正确地将数据传递到Controller?

来自分类Dev

Laravel 5-在routes.php的末尾捕获所有路由(Route :: controller)?

来自分类Dev

Get current route name in Ember

来自分类Dev

Umbraco 7中的Surface Controller或Custom Controller?

来自分类Dev

Laravel 4-Route :: resource vs Route :: controller 使用哪个?

来自分类Dev

访问 Area.Name 引发错误

来自分类Dev

laravel route parameter to controller and returning JSON

来自分类Dev

Laravel 5.3+中的Route :: controller()替代

来自分类Dev

MVC5的嵌套布局

来自分类Dev

MVC5:迭代ViewModel

来自分类Dev

MVC5延迟事件

来自分类Dev

MVC5中的GridView

来自分类Dev

mvc5身份AllowAnonymous

来自分类Dev

嵌套的WebGrid,MVC5

Related 相关文章

热门标签

归档