【问题标题】:How do I refer to a constant URL in my route configuration?如何在路由配置中引用常量 URL?
【发布时间】:2010-05-18 04:31:44
【问题描述】:

假设我在网页中有以下内容

<% 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 });

当用户在下拉列表中选择一个项目时,返回的 URL 是:

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

显然列表中的第一条路线没有被匹配。

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

【问题讨论】:

    标签: asp.net-mvc routing


    【解决方案1】:

    这里的问题是你的路由有默认值

    CourseSelection = (string)null

    这不是路由 URL 的一部分(又名“摘要”)。

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

    所以另一种解决方法是:

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

    但是,由于您将该值发布到操作中,我不明白为什么您将 CourseSelection 作为您的路线中的默认设置。您只需要将它作为操作方法参数,它会在发布的表单数据中自动绑定。

    因此,另一种解决方案是像这样更改您的路线:

    routes.MapRoute(null, "Summary", 
      new { controller = "Summary", action = "ShowData" });
    

    【讨论】:

      【解决方案2】:

      当你查看它的 html 源代码时,

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

      您会注意到该操作是空的。这是因为助手无法根据您在 BeginForm 中提供的内容找到路线。

      根据您在 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 }); 
      
      }
      

      摘要控制器

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

      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>
      <% } %>
      

      }

      【讨论】:

      • 抱歉回复延迟。我一直在做更多的研究,但我仍然在努力理解自定义路由在 mvc 中的工作原理。我已经编辑了我的原始帖子。你还能帮忙吗?
      猜你喜欢
      • 2014-04-15
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多