问题
I am new in Asp.net MVC. I am struck at my Customize URL Routing problem. I have created my Controller named "Customer" and action as "DisplayCustomer". In Global.asax.cs page,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Customer",
action = "DisplayCustomer",
id = UrlParameter.Optional
}); // Parameter defaults//, new { Code = @"\d{1001,1002}" }
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
I don't know what wrong in it,it is always showing as
http://localhost:50415/Views/Customer/DisplayCustomer.aspx
not as
http://localhost:50415/Customer/DisplayCustomer
Am I missing something to make it this to work?
回答1:
Replicated your problem
- Select Project Properties
- Select Web Tab
Now, See the highlighted part in screen shot below
Original Version

Modified Version

Removed the View Directory and .cshtml extension. This happened because you set the start Page.
Selecting the Current Page will fix your issue

回答2:
This url: http://localhost:50415/Views/Customer/DisplayCustomer.aspx
is probably opening up in your Browser when you run/debug your project while the current active document in Visual Studio is the DisplayCustomer
view, try to close this document and re-run the project (i think the problem is fixed for MVC 4 projects).
More importantly, your Routes
are not correctly defined. if you want to access your Action
using the default pattern ({Controller}/{Action}
and in your case /Customer/DisplayCustomer
) you don't have to write any custom route, just leave the default route as is:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL pattern
new { Id = UrlParamter.Optional });
Now, the standard way to get the Url pointing to your Action
(from within your Controller/View) is to use the Url.Action
method. for example: Url.Action("DisplayCustomer", "Customer")
.
回答3:
Change "Views" to "Default"
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Customer",
action = "DisplayCustomer",
id = UrlParameter.Optional
}); // Parameter defaults
}
来源:https://stackoverflow.com/questions/17802820/customize-url-in-mvc-not-working