Asp.net mvc 在MVC中自定义URL不起作用

Asp.net mvc 在MVC中自定义URL不起作用,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Asp.net Mvc 2,我是Asp.NETMVC的新手。我对自定义URL路由问题感到震惊。 我已经创建了名为“Customer”的控制器和名为“DisplayCustomer”的操作。 在Global.asax.cs页面中 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApp

我是Asp.NETMVC的新手。我对自定义URL路由问题感到震惊。 我已经创建了名为“Customer”的控制器和名为“DisplayCustomer”的操作。 在Global.asax.cs页面中

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);
        }
    }
}
我不知道这有什么不对,它总是表现出来 作为

不如

http://localhost:50415/Customer/DisplayCustomer

我是不是错过了一些可以让它正常工作的东西?

这个url:
http://localhost:50415/Views/Customer/DisplayCustomer.aspx运行/调试项目时,
可能会在浏览器中打开,而Visual Studio中的当前活动文档是
DisplayCustomer
视图,尝试关闭此文档并重新运行项目(我认为MVC4项目的问题已经解决)

更重要的是,您的
路线没有正确定义。如果您想使用默认模式(
{Controller}/{Action}
访问您的
操作
,在您的情况下,
/Customer/displaycuster
),您不必编写任何自定义路由,只需保留默认路由:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL pattern
    new { Id = UrlParamter.Optional });
现在,从控制器/视图中获取指向
操作的Url的标准方法是使用
Url.Action
方法。例如:
Url.Action(“displaycuster”、“Customer”)
复制了您的问题

  • 选择项目属性
  • 选择Web选项卡
  • 现在,请参见下面屏幕截图中突出显示的部分

    原始版本

    修改版本

    删除了视图目录和.cshtml扩展名。这是因为您设置了起始页

    选择当前页面将解决您的问题

    将“视图”更改为“默认”


    如何创建此url?在带有@Html.Action的视图中?在这种情况下,默认路由足以创建所需的url格式。请在ASP.NET MVC Internet模板中查看此文件:App_Start\RouteConfig.cs如果手动键入所需的URL会发生什么情况?@glosrob:如果手动键入,则会起作用URL@Andreas:我已创建此URL。如果我手动键入means,则它正在工作,breaking正在点击App_start,它显示添加了路由,但它没有指向路由。我正在使用MVC 2进行尝试。我关闭了doument并尝试了,但仍然不工作。在MVC 2中是否有任何解决方法。你说的“不工作”是什么意思?当你运行这个项目的时候发生了什么?另外,请显示完整的
    Global.asax
    。添加了完整的Global.asax。正如下面PKKG所建议的,我删除了这些web属性,执行了,URL不是点,它只是显示为,如果我在web属性中提到,它指向的很好。如果我在属性中指定URL,它指向的很好。但是将采用App_开始路径或此属性URL。@MahaSwetha:但是会采用App_开始路径或此属性URL吗?,如果我在“属性”中提到了它,那么它的路由很好。否则它就不起作用了。@MahaSwetha:这是因为您可能已经设置了起始页
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL pattern
        new { Id = UrlParamter.Optional });
    
    routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
     new {
       controller = "Customer",
       action = "DisplayCustomer",
       id = UrlParameter.Optional
         }); // Parameter defaults
     }