Asp.net 当我不使用';我不想这样

Asp.net 当我不使用';我不想这样,asp.net,asp.net-mvc,url-routing,asp.net-mvc-routing,Asp.net,Asp.net Mvc,Url Routing,Asp.net Mvc Routing,在“我的布局”页面中,指向构成我的站点的主要部分的链接通过如下调用呈现: @SiteSectionLink("index", "blog", "blog") @helper SiteSectionLink(string action, string controller, string display) { <li> <h1> <a class="site-section" href="@Url.Action(action, contr

在“我的布局”页面中,指向构成我的站点的主要部分的链接通过如下调用呈现:

@SiteSectionLink("index", "blog", "blog")
@helper SiteSectionLink(string action, string controller, string display)
  {
  <li>
    <h1>
      <a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
  </li>
}
<a class="site-section" href="@Url.Action(action, controller, 
  new { date = (string)null, postID = (int?)null})">@display</a></h1>
@helper SiteSectionLink(string action, string controller, string display, string date = null, string id=null)
{ 
  <li> 
    @if (date == null)
    {
        <h1><a class="site-section" href="~/blog/@controller/@action">@display</a></h1> // simple workaround or better use P. Haack workaround
    }
    else 
    {
        <h1><a class="site-section" href="@Url.RouteUrl("blog", new { action = @action, controller = @controller, date = @date, id = @id })">@display</a></h1> 
    }
  </li> 
} 
其中
SiteSectionLink
是一个如下所示的帮助器:

@SiteSectionLink("index", "blog", "blog")
@helper SiteSectionLink(string action, string controller, string display)
  {
  <li>
    <h1>
      <a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
  </li>
}
<a class="site-section" href="@Url.Action(action, controller, 
  new { date = (string)null, postID = (int?)null})">@display</a></h1>
@helper SiteSectionLink(string action, string controller, string display, string date = null, string id=null)
{ 
  <li> 
    @if (date == null)
    {
        <h1><a class="site-section" href="~/blog/@controller/@action">@display</a></h1> // simple workaround or better use P. Haack workaround
    }
    else 
    {
        <h1><a class="site-section" href="@Url.RouteUrl("blog", new { action = @action, controller = @controller, date = @date, id = @id })">@display</a></h1> 
    }
  </li> 
} 
现在的问题是,当我点击一个类似于“blog/11-2010”或“blog/11-2010/253”的链接时,我的布局页面中的链接(通常指我的博客)现在也指同一个URL,当我希望它只是链接到“blog/”而不是“blog/11-2010”

如果我将SiteSectionLink帮助器更改为显式地为
date
postID
传递null,如下所示:

@SiteSectionLink("index", "blog", "blog")
@helper SiteSectionLink(string action, string controller, string display)
  {
  <li>
    <h1>
      <a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
  </li>
}
<a class="site-section" href="@Url.Action(action, controller, 
  new { date = (string)null, postID = (int?)null})">@display</a></h1>
@helper SiteSectionLink(string action, string controller, string display, string date = null, string id=null)
{ 
  <li> 
    @if (date == null)
    {
        <h1><a class="site-section" href="~/blog/@controller/@action">@display</a></h1> // simple workaround or better use P. Haack workaround
    }
    else 
    {
        <h1><a class="site-section" href="@Url.RouteUrl("blog", new { action = @action, controller = @controller, date = @date, id = @id })">@display</a></h1> 
    }
  </li> 
} 

当前路线值仍在使用,但现在看起来像“blog?date=11-2010”


我看到了类似的问题,但被接受的答案对我不起作用,首先,我不使用
ActionLink
,我怀疑
ActionLink
会在引擎盖下使用
Url.Action

而您遇到的问题与Phil Haack在关于MVC3路由错误和带有两个可选参数的路由时详细描述的行为不同,我建议应用Phil帖子中描述的修复方法


我还建议不要创建具有两个可选参数的路由,而是遵循将所需路由分为两个单独路由的模式。

虽然您遇到的问题与Phil Haack在关于MVC3路由错误和具有两个可选参数的路由的行为中所述的不太一样,我建议应用Phil帖子中描述的修复方法


我还建议不要创建带有两个可选参数的路由,而是遵循将所需路由分为两个单独路由的模式。

是Url。操作方法将参数放在查询字符串中。 您可以按如下方式更改辅助对象:

@SiteSectionLink("index", "blog", "blog")
@helper SiteSectionLink(string action, string controller, string display)
  {
  <li>
    <h1>
      <a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
  </li>
}
<a class="site-section" href="@Url.Action(action, controller, 
  new { date = (string)null, postID = (int?)null})">@display</a></h1>
@helper SiteSectionLink(string action, string controller, string display, string date = null, string id=null)
{ 
  <li> 
    @if (date == null)
    {
        <h1><a class="site-section" href="~/blog/@controller/@action">@display</a></h1> // simple workaround or better use P. Haack workaround
    }
    else 
    {
        <h1><a class="site-section" href="@Url.RouteUrl("blog", new { action = @action, controller = @controller, date = @date, id = @id })">@display</a></h1> 
    }
  </li> 
} 

是Url。Action方法将参数放入querystring中。 您可以按如下方式更改辅助对象:

@SiteSectionLink("index", "blog", "blog")
@helper SiteSectionLink(string action, string controller, string display)
  {
  <li>
    <h1>
      <a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
  </li>
}
<a class="site-section" href="@Url.Action(action, controller, 
  new { date = (string)null, postID = (int?)null})">@display</a></h1>
@helper SiteSectionLink(string action, string controller, string display, string date = null, string id=null)
{ 
  <li> 
    @if (date == null)
    {
        <h1><a class="site-section" href="~/blog/@controller/@action">@display</a></h1> // simple workaround or better use P. Haack workaround
    }
    else 
    {
        <h1><a class="site-section" href="@Url.RouteUrl("blog", new { action = @action, controller = @controller, date = @date, id = @id })">@display</a></h1> 
    }
  </li> 
} 

谢谢,我添加了一个可选的
routeValues
参数到
SiteSectionLink
,该参数将日期和postID设置为null。我仍然很好奇为什么它会这样,什么时候需要这种行为?
Url.Action
方法在为与段变量不对应的属性提供值时使用querystring参数。在您的情况下,我认为这是由于mvc引擎解码路由“blog”的url的方式,匹配匿名类型中未传递的参数。感谢您,我向
SiteSectionLink
添加了一个可选的
routeValues
参数,将日期和posted设置为null。我仍然很好奇为什么它会这样,什么时候需要这种行为?
Url.Action
方法在为与段变量不对应的属性提供值时使用querystring参数。在你的情况下,我认为这是由于mvc引擎解码路由“blog”的url的方式,匹配匿名类型中未传递的参数。感谢你在拆分路由方面的改进,你是对的,有两个选项只会引起麻烦。再想一想,我之所以将此标记为答案,是因为拆分解决了这个问题,再加上为
SiteSectionLink
添加了一个重载。感谢您在拆分路由方面所做的改进,您是对的,有两个选项只会引起头痛。再想一想,我之所以将此标记为答案,是因为拆分解决了这个问题,并将重载添加到
SiteSectionLink