Is it required to have “area” routevalue on actionlink if the application was grouped into areas?

风流意气都作罢 提交于 2019-12-05 01:37:54

Yes, if you're working with areas you should always specify an Area in ActionLink links, an empty one if you don't want the link to go to a specific area, like this:

Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })

This is needed because otherwise, if you don't specify an Area, the one where the user is in at the moment will be used.

If you for instance use an ActionLink without specifying an Area in your _Layout.cshtml page, it will work as long as you stay in the root of your Application. From the moment you go into an area though, the link will be generated as \currentArea\the_rest_of_the_link, and hence, won't work anymore.

I prefer RouteLink method since it will render it exactly.

@Html.RouteLink( 
      "Home Page", 
      "Default", 
      new 
      { 
         Action = "Index", 
         Controller = "Home" 
      } 
)

Give it a shot.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!