【问题标题】:How to give duplicate name in the url bar如何在网址栏中提供重复的名称
【发布时间】:2012-12-13 05:47:24
【问题描述】:

如何复制网址栏中的名称。例如我有products controllerindex method。现在在我的项目中,我像localhost:89733/Products/Index 一样运行。但不是Products/Index,而是想给另一个名字来运行相同的action method。我怎样才能做到这一点。我的意思是我不想将我的控制器信息泄露给其他人。任何帮助表示赞赏。

【问题讨论】:

    标签: asp.net-mvc-3 url c#-4.0 asp.net-mvc-routing


    【解决方案1】:

    您可以配置自定义路由:

    routes.MapRoute(
        "ProductsRoute",
        "mycustomproductsname/{id}",
        new { controller = "Products", action = "Index", id = UrlParameter.Optional }
    );
    

    现在,当您导航到 mycustomproductsname/123 时,Products 控制器的 Index 操作将被执行,并将 123 作为 id 参数传递。

    【讨论】:

    • 假设如果我们有 10 个控制器和 10 个索引页面,那么我们需要添加 10 个地图路由。对于每个操作方法,我需要添加另一个地图路线。什么是“ProductRoute”
    • "ProductRoute" 是路由的名称。每条路线都应该有一个唯一的名称。就重复而言,您可能有一些模式,其中控制器名称应该以某种方式成为路由的一部分。例如:"mycustom{controller}sname/{id}"。这样控制器名称可以在 url 中传递。如您所见,Index 不再是 url 的一部分,而是被推断出来的。但是,如果您不想在 url 中有任何关于任何控制器名称的指示,则必须进行单独的路由映射。如果你问我这样做的全部目的是完全没有意义的......
    • 但是我已经实现了你在注册路由功能中所说的。我已经用新的 url 名称运行了它,但它不工作
    • 您是否删除了默认路由?还要定义不工作?你在地址栏中输入了什么网址?您的MapRoute 看起来如何?
    • 默认路由应该放在您的自定义路由之后,因为它不太具体。路由引擎按照您定义它们的相同顺序评估路由。所以总是把更具体的路线放在顶部。
    【解决方案2】:

    我希望 Routing 可以解决 Global.asax 文件应用启动事件中的目的

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);
    
    
    }   
    
    
    public void RegisterRoutes(RouteCollection routes)
    {
        //Treeview routing
        routes.MapPageRoute("Home", "User/Dashboard", "~/Products/Index");
        routes.MapPageRoute("SearchCustomer", "User/Search-Customer", "~/Products/SearchCustomer");
    
    
    
    } 
    

    我希望你提出这个问题,它对你有帮助

    【讨论】:

    • 在上面的例子中“~/Products/Index”指的是用户/仪表板,不是吗
    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2016-10-29
    • 2013-09-15
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多