0

Hi I am using areas in my asp.net mvc 4 application i have multiple areas and I am navigating from an area to another , How can I remove the area name from the Url like this :

.../AreaName/ControllerName/ActionName To .../ControllerName/ActionName

for example :

"ActionLink("Text","ActionNAme","ControllerName",new{area = "area name"},null)"

sometimes I am redirecting to some view inside other areas after a certain process like the following:

"ReturnRedirectToAction("Text","ActionName","ControllerName",new{area ="areaName"}, null)"

Like this everything works fine but i dont want the AreaName to appear in the Url.

here's an AreaRegistration class:

public class ChileAreaAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "ChileArea";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "ChileArea_default",
                "ChileArea/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
Haytham
  • 834
  • 1
  • 12
  • 31
  • Does this involve e.g. `Html.ActionLink(...)`? Please show a bit of code. – Peter B Mar 28 '18 at 16:20
  • Possible duplicate with https://stackoverflow.com/a/23388720/311063 – Miro J. Mar 28 '18 at 16:25
  • @PeterB , i Updated my question . – Haytham Mar 28 '18 at 16:29
  • Possible duplicate of [ASP.NET MVC ActionLink outside area](https://stackoverflow.com/questions/3756420/asp-net-mvc-actionlink-outside-area) – Peter B Mar 28 '18 at 16:30
  • Thank you for your responses but it is not a duplicate , my goal is to get rid of the area name in the url , my navigation is OK i ve no problem navigating from an area to another , my goal is to remove the area name for URLs – Haytham Mar 28 '18 at 16:33
  • Is your goal to move the *regular* non-area controllers into one of your areas? If not, there will be no way for MVC to differentiate between your area routes and non-area routes. Also, only 1 area could be configured without an area name in order to be able to differentiate between the other areas. Is that what you are aiming for? – NightOwl888 Mar 28 '18 at 17:46

2 Answers2

0

If you want to totally get rid of the Area part for one or more Controllers - meaning it/they should no longer belong to an Area, then you'll have to move the *Controller.cs + corresponding Views and Models, and possibly corresponding DisplayTemplates/EditorTemplates, into the Controllers / Views / Models folders at the root of the project.

If you are using VisualStudio then you can use drag-n-drop for this.

It would be wise to also adjust the namespace for every .cs-file that was moved, for clarity and consistency, and adjust all using accordingly.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • I can't do that because I've to use Areas. But thank you for the answer. – Haytham Mar 28 '18 at 17:47
  • Then what you want is impossible, AFAIK. The area determines where both the Controller and the Views etc. are found. You can map an 'area' Controller to the root of the site, but then the Controller can't find it's Views anymore - unless perhaps you move those files (and I guess it doesn't make sense to move those, but not everything else). – Peter B Mar 29 '18 at 07:59
0

Put this decorator at controller definition

[RouteArea("AreaName", AreaUrl = "")]
Jordi Jordi
  • 461
  • 3
  • 10
  • I tried that but i didn't work it gave me "404 Not found" when trying to access an action in another controller of a different area. '[But it work when i remove that attribut and] but in that case i keep getting the area name in the UR which is originally my problem. – Haytham Mar 28 '18 at 17:50