代码之家  ›  专栏  ›  技术社区  ›  Alex P

如何在路由配置中引用常量URL?

  •  1
  • Alex P  · 技术社区  · 14 年前

    假设我在一个网页内有以下内容

    <% using (Html.BeginForm("ShowData", "Summary")) %>
    <% { %>
    <div class="dropdown"><%=Html.DropDownList("CourseSelection", Model.CourseList, new { @class = "dropdown",  onchange="this.form.submit();" })%> </div>
    <% } %>
    

    当用户从下拉列表中进行选择时,表单将被提交,我希望它链接到另一个具有以下URL的页面:

    http://localhost:1721/Summary
    

    我有以下路线:

        routes.MapRoute(null, "Summary", new { controller = "Summary", action = "ShowData", CourseSelection = (string) null });
    
        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional });
    

    当用户在DropDownList中选择一个项目时,返回的URL是:

    http://localhost:1721/Summary/ShowData?CourseSelection = UserSelection
    

    显然,列表中的第一条路线不匹配。

    我不希望URL显示操作名称和参数。我只想显示“摘要”,这是我在URL中硬编码的内容。我如何做到这一点?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Haacked    14 年前

    这里的问题是您的路由有一个默认值

    课程选择=(字符串)空

    它不是路由URL的一部分(也称为“摘要”)。

    当生成一个URL时,有一个特殊的逻辑,即路由的任何默认值,其中参数是 在URL中,指定的参数必须与默认值匹配。

    另一种解决方法是:

    using (Html.BeginForm("ShowData", "Summary", 
      new {CourseSelection = (string)null})) {
      ...
    }
    

    但是,由于你将这个值发布到动作中,我不明白为什么在你的路线中有默认的课程选择。您只需要它作为一个操作方法参数,当它在已发布的表单数据中时,它将自动被绑定。

    所以另一种解决方案是像这样改变你的路线:

    routes.MapRoute(null, "Summary", 
      new { controller = "Summary", action = "ShowData" });
    
        2
  •  0
  •   Raj Kaimal    14 年前

    当您查看此的HTML源代码时,

     <% using (Html.BeginForm("UpdateView", "MyController")) %> 
        <% { %> 
        <div class="dropdown"><%=Html.DropDownList("Selection", Model.List, new { onchange="this.form.submit();" })%></div>          
        <% } %> 
    

    您将注意到操作是空的。这是因为帮助者无法根据您在begininform中提供的内容找到路由。

    根据global.asax中的定义,所有请求都将默认为index操作方法。相反,你想要的是:

     routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index" });  
    

    请注意URL模式中操作的附加项。

    这对我有用。

    全球.asax

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(null, "Summary", new { controller = "Summary", action = "ShowData", CourseSelection = (string)null });
    
        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional }); 
    
    }
    

    汇总控制器

    公共类摘要控制器:控制器 { / / //获取:/摘要/

    public ActionResult Index()
    {
        return View();
    }
    
    public ActionResult ShowData(string CourseSelection)
    {
        return View();
    }
    

    默认视图中的代码

    <%
        var list = new List<string> { "a", "b", "c" };
    
        var selList = new SelectList(list); %>
    <% using (Html.BeginForm("ShowData", "Summary")) %>
    <% { %>
    <div class="dropdown">
        <%=Html.DropDownList("CourseSelection", selList, new { @class = "dropdown",  onchange="this.form.submit();" })%>
    </div>
    <% } %>
    

    }