【问题标题】:Html.Action() results in "No route in the route table matches the supplied values"Html.Action() 导致“路由表中没有路由与提供的值匹配”
【发布时间】:2017-06-28 06:44:47
【问题描述】:

这个问题已经讨论过很多次了,但我还没有找到适合我的特殊情况的解决方案。

在我的 Umbraco (6) 视图之一中,我正在使用调用控制器方法

@Html.Action("Index", "CountryListing");

这会导致“路由表中没有路由”异常。

我一直在摆弄 RegisterRoutes 方法,但无济于事。我想知道当我清空 RegisterRoutes 方法时,它是否甚至被用作站点仍然可以运行。这是现在的样子:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

我也试过像这样在通话中添加一个空的“区域”

@Html.Action("Index", "CountryListing", new {area: String.Empty});

我在其他地方使用@Html.Action 语句并且它们确实有效,所以我明白为什么其中一些有效而另一些无效,但现在的主要问题是让我的国家/地区列表操作有效。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing umbraco6


    【解决方案1】:

    您可以通过以下方式解决此问题

    1. 确保您的控制器正在扩展 Umbraco 的 SurfaceController
    2. 将其命名为 YourName*SurfaceController*
    3. 将 [PluginController("CLC")] 'annotation'(我来自 Java)添加到您的控制器。 CLC 代表 CountryListController。你当然可以自己起名字。
    4. 将 PluginController 名称 (CLC) 作为“Area”参数添加到 Html.Action 调用中。

    我的控制器:

    [PluginController("CLC")]
    public class CountryListingSurfaceController : SurfaceController
    {
        public ActionResult Index()
        {
           var listing = new CountryListingModel();
    
           // Do stuff here to fill the CountryListingModel
    
           return PartialView("CountryListing", listing);
        }
    }
    

    我的部分观点(CountryListing.cshtml):

    @inherits UmbracoViewPage<PatentVista.Models.CountryListingModel>
    
    @foreach (var country in Model.Countries)
    {
        <span>More razor code here</span>  
    }
    

    动作调用:

    @Html.Action("Index", "CountryListingSurface", new {Area= "CLC"})
    

    【讨论】:

      【解决方案2】:

      你可以使用 null 而不是使用 String.empty

      @Html.Action("Index", "CountryListing",null);
      

      如果您使用区域,则必须为每个区域覆盖 RegisterRoutes

      public override string AreaName
          {
              get
              {
                  return "CountryListing";
              }
          }
      
          public override void RegisterArea(AreaRegistrationContext context)
          {
              context.MapRoute(
                  "CountryListing_default",
                  "CountryListing/{controller}/{action}/{id}",
                  new { action = "Index", id = UrlParameter.Optional }
              );
          }
      

      我建议你看看同样的问题: https://stackoverflow.com/a/11970111/2543986

      【讨论】:

      • User2543...感谢您的回复。不幸的是,添加 null 并没有解决问题,我不明白你的其他解决方案。然而,与此同时,我找到了解决方案。我认为这个问题与 Umbraco 有关......(虽然它也是一个通用的 MVC 问题)..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多