1

I'm making a small project that has a page that shows a list of applications available to download. My routing in RouteConfig.cs looks like this:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "ViewApplication",
            url: "View/{applicationname}",
            defaults: new { controller = "View", action = "ViewApplication"}
        );

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

Where my controller looks like this:

public class ViewController : Controller
{
    public ActionResult ViewApplication(string applicationname)
    {
        return View();
    }
}

But whenever I try to navigate to localhost:50788/View/A610723 it fails, and the URL changes to localhost:50788/? and stays on the home page.

I've had a look at this question MVC 4: Custom Routes And its almost exactly the same as what I want to do, where they are using beername as a string, but mine isn't working.

Is there something I've missed?

Thanks

1 Answer 1

1

Your solution seems to be correct. Are you sure your error is not somewhere else?

Here is a little example from this link:

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

It looks exactly like your solution.

using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1 {
public class MvcApplication : System.Web.HttpApplication {

    public static void RegisterRoutes(RouteCollection routes) { 

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute( 
          "Blog", // Route name 
          "Archive/{entryDate}", // URL with parameters
           new { controller = "Archive", action = "Entry" } // Parameter defaults 
           );

           routes.MapRoute( "Default", // Route name 
           "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = "" } // Parameter defaults
           ); 
    } 

    protected void Application_Start() {

          RegisterRoutes(RouteTable.Routes);

    } 
  }
}

And here is the Controller:

 using System; using System.Web.Mvc;

 namespace MvcApplication1.Controllers { 
 public class ArchiveController : Controller { 

     public string Entry(DateTime entryDate) { 
          return "You requested the entry from " + entryDate.ToString(); 
     }
   } 
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks the confirmation, I was just worried I missed something obvious like a spelling error or a wrong definition. I really can't find any reason why it doesn't route correctly, I'm typing the url directly into the browser, so surely only the router and controller are involved?
Well I think so. Did you try to copy the example i mentioned? Did it work? I'll test your code this evening to see if i get the same error.
You were right, the problem was elsewhere. I had a typo in the name of the ViewApplication.cshtml file. Thank you so much for your 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.