Controllers and page names

This is practically my first day in this after an intensive course last week, so I’m a little ropey when it comes to terms, but have spotted an issue as i practice and wondered if there was a way out of it.

I have some controllers, and they are calling the correct .cshtml file depending on what button is clicked, I have a this bit of code below that displays a page at the moment called ‘FunnyBones’

<div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Funny Bones", "FunnyBones", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
            </div>

When the button is clicked at the moment the url is - http://localhost:55666/FunnyBones, but what I need really is http://localhost:55666/Funny-Bones

So below I tried to set it like this

        public ActionResult Funny-Bones()
        {
            ViewBag.Message = "This is funny bones.";

            return View();
        }

But the dash between Funny and Bones causes an error, and that is calling at the moment a .cshtml file called FunnyBones.cshtml, when ideally I need it to have the dash.

As you will probably know, changing FuunyBones() to Funny-Bones() crashes it in HomeController.cs

I hope that makes sense

In .NET, you’re not allowed to use dashes. It should be FunnyBones, or Funny_Bones.

Edited by cpradio: Removed self promotion

@multichild, you simply have to use an attribute.

So instead of

        public ActionResult Funny-Bones()
        {
            ViewBag.Message = "This is funny bones.";

            return View();
        }

You would have:

        [ActionName("Funny-Bones")] 
        public ActionResult FunnyBones()
        {
            ViewBag.Message = "This is funny bones.";

            return View();
        }

P.S., Sorry it took so long to get you an answer, it has been a crazy past couple of days for me.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.