Sitefinity 8.1 custom MVC routing not working
Asked Answered
T

1

6

After update to V8.1 from V6.1, our MVC custom code not working, it returned 404 (custom code is some APIs read content and commerce data using Sitefinity APIs).

According to documentation "here" , it said that "Bootstrapper.MVC.MapRoute is removed. Call the RouteTable.Routes.MapRoute (System.Web.Mvc)instead." , so I changed my code from

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 = "" }
           );
}

to

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

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

But the routing still not working.

Here is a sample of our MVC classes:

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

And here is how we bind routing in global.ascx:

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

Any idea how can we resole that?

Tsingyuan answered 20/7, 2015 at 13:54 Comment(0)
T
5

I got the below advice from Sitefinity Support, I think it's working well now.

Regarding this issue, try to move the route registration in the HttpApplication global class like:

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

And also in the "baseApi" try to avoid using the prefix "ext" as such prefix is used by Sitefinity and might have some issues.

Tsingyuan answered 22/7, 2015 at 15:24 Comment(4)
Can you explain what problems might be caused from: "And also in the 'baseApi' try to avoid using the prefix 'ext' as such prefix is used by Sitefinity and might have some issues."? And how this is related to your question?Insolation
The problem that we were call the "RegisterRoutes" from Global.asax.cs "Application_Start()". but it should be moved to event handler of "Bootstrapper.Initialized" ==> in our case "Bootstrapper.Initialized += OnSitefinityAppInitialized" , besided that it should be executed only when a command name like "e.CommandName == "RegisterRoutes"". Actually I don't know the exact reason for that, but this was the recommendation by Sitefinity Support team, and that solution solved the above problem/ and now all my APIs and MVC calls working good again in sitefinity v8.1.Tsingyuan
regarding the prefix 'ext', actually it was recommended by support team to be changed, but I didn't change it and it's working good with me.Tsingyuan
I understood your answer about handling the Bootstrapper event. To register WebApi routes you have to do the same thing (you can also use (e.CommandName == "Bootstrapped") FWIW). I just don't think that last sentence about your route key "prefix" is relevant to the question. The only issue that could potentially occur is if Telerik decided to add a route with the same key in a future version, which would be a potential problem regardless of the key you chose.Insolation

© 2022 - 2024 — McMap. All rights reserved.