如何从区域控制器设置主应用程序控制器的url.actionlink。 [英] How to set the url.actionlink for main application controller from area controller.

查看:102
本文介绍了如何从区域控制器设置主应用程序控制器的url.actionlink。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个MVC 5应用程序工作,其中我有主应用程序,并且在同一个应用程序中有两个区域(管理员,计费)。主应用程序,管理区域和计费区域中有HomeController。我在主应用程序RouteConfig.cs文件中设置了Billing Area Billcontroller的默认路由:见下文:

i am working for a MVC 5 application, in which i have the main application, and there is two Area(Admin,Billing) in same application. there is HomeController in Main application, Admin Area and Billing Area. i have set the default route for Billing Area Billcontroller in main application RouteConfig.cs file: see below:

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 },
          //    namespaces: new string[] { "MvcAreaDemoApp.Controllers" }
          //);

          routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Bill", action = "Index", id = UrlParameter.Optional }
          , namespaces: new string[] { "MvcAreaDemoApp.Controllers" }
         ).DataTokens["area"] = "Billing";
      }



当我试图进入主应用程序家庭控制器方法时,我没有重定向到主应用程序的主控制器,它会去同区域的家用控制器方法。



我尝试过的方法:




When i am trying to go in main application Home controller method, i not redirecting to home controller for main application, it's going to same areas homecontroller method.

What I have tried:

<a href="@Url.Action("Index","Home", new { area="" })">Dashboard</a>

推荐答案

我认为你不需要new {area =}段

所以只是,

I don't think you need the "new {area = ""}" segment
so just,
<a href="@Url.Action("Index","Home")">Dashboard</a>



如果你正在使用区域,那么你需要使用像


if you are using areas then you need to use something like

@Url.Action("Method", "Controller", new {Area = "AreaName"})



当您加载页面时,您始终可以检查网址的位置通过使用浏览器的开发人员工具指向。例如,在Chrome上点击右键检查

如果URL正如您所期望的那样且仍然无效,则可能是您的路由错误。我看起来像这样


when you load the page up you can always check where the url is pointing to by using the browser's developer tools. eg on chrome right click inspect
If the URL is as you are expecting and it is still not working then it is probably your routing that is wrong. Mine looks like this

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


这篇关于如何从区域控制器设置主应用程序控制器的url.actionlink。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆