3

I want to create a URL similar to: localhost:port/farm/animals/cow.cshtml. How do I create a controller for pages behind /animals/ ?

If I create a standard:

public class FarmController : Controller
{
    //
    // GET: /Farm/

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

I will not get there because the cow.cshtml is behind the animals. Nor will I get to cow.cshtml with the following code:

public class AnimalsController : Controller
{
    //
    // GET: /Animals/

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

Because the link to cow.cshtml is not /animals/ but /farm/animals/cow.

How can I solve this issue?

3
  • Does it have to be MVC4? Can you use MVC5? Commented May 7, 2015 at 13:39
  • Sounds like a routing issue, this tutorial might help. Commented May 7, 2015 at 13:42
  • Sorry, the tag does imply I use MVC4 but yes I use MVC5 :) Commented May 7, 2015 at 13:50

2 Answers 2

4

You need to register a route like the following -

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Animals",
            url: "farm/animals/{action}",
            defaults: new { controller = "Farm" }
        );

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

Controller

public class FarmController : Controller
{
    // GET: Farm
    public ActionResult Index()
    {
        return View();
    }

    // GET: Farm/Animals
    public ActionResult Animals()
    {
        return View();
    }

    // GET: Farm/Animals/Cow
    public ActionResult Cow()
    {
        return View();
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Would I then use the Farmcontroller to get to cow.cshtml, or use the animalscontroller?
Having both FarmController and Animal Controller does make sense. You just need to user FarmController. I updated the answer.
I've done exactly that. However when I try to go to /farm/animals/cow I actually get send to the animals.cshtml in /farm/.
Did you register the route?
Cow.cshtml should be located next to Animals.cshtml and Index.cshtml. Look at the screenshot that I updated. MVC View Folder should not be nested like traditional ASP.Net WebForm; it is the beauty of MVC.
|
1

Whilst routing would solve your problem, this sounds like a job for MVC areas. If in your actual application you're wanting to separate parts of your site, then an area might be the solution here.

Comments

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.