Sitefinity 8.1自定义MVC路由不起作用

问题描述:

从V6.1更新到V8.1后,我们的MVC自定义代码无法工作,它返回404(自定义代码是一些API使用Sitefinity API读取内容和商业数据)。Sitefinity 8.1自定义MVC路由不起作用

根据文档“here”,它表示“Bootstrapper.MVC.MapRoute被删除,而是调用RouteTable.Routes.MapRoute(System.Web.Mvc)”。 ,所以我改变了我的代码

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

    Bootstrapper.MVC.MapRoute(
      "ExternalAccess", 
      "baseApi/{controller}/{action}/{id}", 
      new { controller = "MvcMainApiCntr", action = "Index", id = "" } 
      ); 
} 

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

    routes.MapRoute(
     "ExternalAccess", 
     "baseApi/{controller}/{action}/{id}", 
     new { controller = "MvcMainApiCntr", action = "Index", id = "" } 
     ); 
} 

但路由仍然没有工作。

这里是我们的MVC类的一个示例:

using System; 
using System.IO; 
using System.Net; 
using System.Web.Mvc; 
using HtmlAgilityPack; 
using Telerik.Sitefinity.Abstractions; 

namespace SitefinityWebApp.Mvc.Controllers 
{ 
    public class SharedAssetsController : Controller 
    { 
     [HttpGet] 
     public ViewResult GetScripts() 
     { 
      var rootUrl = anyfunction(); 
      return View("Scripts", (object) rootUrl); 
     }   
    } 
} 

这里是我们如何绑定在global.ascx路由:

protected void Application_Start(object sender, EventArgs e) 
{ 
    RouteConfig.RegisterRoutes(RouteTable.Routes); //the first method in that post 
    Bootstrap.BootstrapSitefinity(); 
} 

任何想法,我们如何能换鞋底吗?

我从Sitefinity支持获得了下面的建议,我认为它现在运行良好。

关于这个问题,尝试移动路径登记在HttpApplication全局类,如:

void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e) 
{ 
    if (e.CommandName == "RegisterRoutes") 
    { 
     RegisterRoutes(RouteTable.Routes); 
    } 
} 

而且还可以在“baseApi”尽可能尝试这样的前缀用于通过避免使用前缀“转” Sitefinity可能有一些问题。

+0

你能解释一下可能导致什么问题:“并且在'baseApi'中,尽量避免使用前缀'ext',因为这样的前缀被Sitefinity使用,并且可能有一些问题。”?这与你的问题有什么关系? –

+0

我们称之为Global.asax.cs“Application_Start()”中的“RegisterRoutes”的问题。但它应该被移动到“Bootstrapper.Initialized”==>的事件处理程序中,在我们的例子“Bootstrapper.Initialized + = OnSitefinityAppInitialized”中,除了它应该仅在命令名称如“e.CommandName ==”RegisterRoutes“ ”。 其实我不知道确切的原因,但这是Sitefinity支持团队的建议,该解决方案解决了上述问题/现在我所有的API和MVC调用在sitefinity v8.1中都能很好地工作。 –

+0

关于前缀'ext',实际上它是由支持团队推荐改变的,但我没有改变它,它对我很好。 –