【问题标题】:Sitefinity 8.1 custom MVC routing not workingSitefinity 8.1 自定义 MVC 路由不起作用
【发布时间】:2015-07-20 13:54:50
【问题描述】:

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

根据文档“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();
}

知道我们该如何解决这个问题吗?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 sitefinity


    【解决方案1】:

    我从 Sitefinity Support 获得了以下建议,我认为它现在运行良好。

    关于这个问题,尝试在HttpApplication全局类中移动路由注册,如:

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

    并且在“baseApi”中尽量避免使用前缀“ext”,因为 Sitefinity 使用了这样的前缀,并且可能会出现一些问题。

    【讨论】:

    • 你能解释一下可能导致什么问题:“而且在'baseApi'中尽量避免使用前缀'ext',因为Sitefinity使用了这样的前缀并且可能有一些问题。”?这与您的问题有何关系?
    • 我们被称为 Global.asax.cs "Application_Start()" 中的 "RegisterRoutes" 的问题。但它应该移动到“Bootstrapper.Initialized”==> 在我们的例子中为“Bootstrapper.Initialized += OnSitefinityAppInitialized”的事件处理程序,除了它应该只在像“e.CommandName ==”RegisterRoutes“这样的命令名称时执行”。实际上我不知道确切的原因,但这是 Sitefinity 支持团队的建议,该解决方案解决了上述问题/现在我所有的 API 和 MVC 调用在 sitefinity v8.1 中再次运行良好。
    • 关于前缀'ext',实际上是支持团队建议更改的,但我没有更改它,它对我来说很好用。
    • 我理解您关于处理 Bootstrapper 事件的回答。要注册 WebApi 路由,您必须做同样的事情(您也可以使用 (e.CommandName == "Bootstrapped") FWIW)。我只是认为关于您的路由键“前缀”的最后一句话与问题无关。唯一可能发生的问题是,如果 Telerik 决定在未来版本中添加具有相同密钥的路由,无论您选择何种密钥,这都是一个潜在问题。
    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2014-12-10
    • 2016-05-10
    • 1970-01-01
    • 2013-11-04
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多