3

I need my page names to have a dash in the name. E.G our-vision

I'm new to MVC & c# so I may be going about all this wrong.

Here is my controller:

  public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
        //
        // GET: /our-vision/
        public ActionResult ourVision()
        {
            return View();
        }
    }

And then in my views, I have Views/Home/ourVision.cshtml.

When I compile and go to http://localhost/ourVision it works, but when I go to http://localhost/our-vision it does not.

Here is my routing:

  routes.MapRoute(
            "Default", // Route name
            "{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68

2 Answers2

5

You'll need to do a few things in order to achieve that.

First, to achieve our-Vision, you'll need to give your action method the ActionName attribute, like so:

[ActionName("our-Vision")]
public ActionResult ourVision()

Next, you'll have to rename your ourVision.cshtml view to be our-Vision.cshtml

Finally, whenever you're using Url.Action or ActionLink, you need to use our-Vision and not vision, like so:

Url.Action("our-Vision", "Home");
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
1

IMHO

The best way to do this - is define new route in route engine:

routes.MapRoute(
            "OurVision", // Route name
            "our-vision", // URL with parameters
            new { controller = "Home", action = "ourVision" } // Parameter defaults
        );
vchyzhevskyi
  • 753
  • 1
  • 11
  • 20