0

I have two controllers with same name in different namespaces

-Controllers
--AdminCp
---AccountController.cs
--Cp
---AccountrController.cs

And same view location structure

-Views
--AdminCp
---Account
----Login.cshtml
--Cp
---Account
----Login.cshtml

My Account controllers contains method Login

public ActionResult Login()
{
    return View();
}

I also have custom RazorViewEngine

public ExtendedRazorViewEngine()
{
    ViewLocationFormats = new[]
    {
        "~/Views/AdminCp/{1}/{0}.cshtml",
        "~/Views/Cp/{1}/{0}.cshtml",
        "~/Views/{1}/{0}.cshtml",
        "~/Views/Layout/{0}.cshtml",
    };
}

My routing uses MvcCodeRouting

routes.MapCodeRoutes(typeof (Controllers.HomeController));

But when i trying route to the action Login in Cp namespace i am getting view from AdminCp namesapce

http://example.com/cp/account/login ---> /views/admincp/account/login.cshtml

I know that i can pass path locatin in View() But... It looks ugly.

Can somebody help me with this? Thanks in advance!

2
  • 1
    I see you created two folders: AdminCp and Cp. But MVC has built in functionality for this purposes: Areas. By using Areas MVC sorts out these type of things for you Commented Sep 21, 2016 at 6:54
  • Thanks, i will try Commented Sep 21, 2016 at 6:57

1 Answer 1

1

You need to have Areas to implement such function.A good link to start is :

Areas

Also,you need to change your RouteConfig:

context.MapRoute(
    "Default",
    "{area}/{controller}/{action}/{id}",
    new { area = "MyOneWay", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Furtermore,ypou can have your controller in below defined way:

namespace MyApp.Areas.MyOneWay.Controllers
{
    public class HomeController : Controller
    {
        ...
    }
}

namespace MyApp.Areas.MySecondWay.Controllers
{
    public class HomeController : Controller
    {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

But i thought that with MvcCodeRouting i dont heed to create areas
ohhh..didnt checked you used MvcCodeRouting.I have not really worked much with MvcCodeRouting,lets wait for some other expert on this .
I think that ill create copy of project and will try to reimplement structure with areas. Thanks for help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.