Custom routing in asp.net mvc 4

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


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(); 
     }
   } 
 }
链接地址: http://www.djcxy.com/p/57858.html

上一篇: ReturnUrl指向一个ActionResult

下一篇: 在asp.net mvc 4中自定义路由