Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 hyphen in URL
    primarykey
    data
    text
    <p>So i'm trying to employ the use of hyphens in my URLs for readability but so far haven't found a way to make it work with mvc4. </p> <p>I'll start with the code in the controller:</p> <pre><code>public ViewResult Index() { ViewBag.URL = Functions.fetchURL(); return View(); } [HttpPost] public ActionResult Index(LogonModel model, string returnUrl) { //omitted to save space return View(model); } public ActionResult Forgot_Login_Info() { return View(); } [HttpPost] public ActionResult Forgot_Login_Info(RetrievePasswordViewModel model) { //omitted to save space return View(); } </code></pre> <p>So I'm using underscores for the name of the actions as seen with "Forgot_Login_Info" and that is the name of the view as well. For testing I also manually created a view called "Forgot-Login-Info"</p> <p>In my global.asax.cs file i simply have this line for routes</p> <pre><code>RouteConfig.RegisterRoutes(RouteTable.Routes); </code></pre> <p>which is the standard routing line with new projects for MVC4 and this is hooked to the "RouteConfig.cs" file and in that file here is the code I have, which I found in another question on this site.</p> <pre><code>public class RouteConfig { public class HyphenatedRouteHandler : MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-","_"); requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_"); return base.GetHttpHandler(requestContext); } } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.Add( new Route("{controller}/{action}/{id}", new RouteValueDictionary( new { controller = "Default", action = "Index", id = UrlParameter.Optional }), new HyphenatedRouteHandler()) ); } } </code></pre> <p>Lastly here is the code for the "Index" view, which is where I have a link to the "Forgot_Login_Info".</p> <pre><code> &lt;div class="centerBlockItem" style="width:450px; text-align:center"&gt; @Html.ActionLink("Forgot Password?", "Forgot-Login-Info") @Html.ActionLink("Forgot Password?", "Forgot_Login_Info") &lt;/div&gt; </code></pre> <p>Now I have the link 2 ways, 1 using underscore and the other using hyphen. According to what i've read in regards to the code I found for this purpose, Both links should open the "Forgot_Login_Info" view and not the "Forgot-Login-Info" view. But what I'm getting instead is the 2nd link works fine and the action name matches the view files name. But when I click on the first link, I get a 404 error because the system can't find the "Forgot-Login-Info" view file, even though I had manually created a file with that name for testing. </p> <p>One more thing to note, the code in my routeconfig file, In the answer I had found it suggested I comment out the default route code lines and just leave:</p> <pre><code>routes.Add( new Route("{controller}/{action}/{id}", new RouteValueDictionary( new { controller = "Default", action = "Index", id = UrlParameter.Optional }), new HyphenatedRouteHandler()) ); </code></pre> <p>I had done this too and got the same results. Any ideas where I'm screwing things up?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload