+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / ASP.NET MVC 4 Custom View Routing in Sitefinity 7

ASP.NET MVC 4 Custom View Routing in Sitefinity 7

5 posts, 0 answered
  1. David
    David avatar
    10 posts
    Registered:
    20 Jul 2014
    15 Oct 2014
    Link to this post

    Our solution hierarchy is as follows:

    language/doctor-cv/doctorMcr/doctorFullName

    Ex: en\doctor-cv\12345\David

    Now I'd like to map the routing so that when the user just types the name of the view in the url, it automatically maps the url to the corresponding controller

    I.E: localhost:1234\en\doctor-cv\12345\David

    Should map to

    View\DoctorCVPage\Index.cshtml

    Currently, we're using the default routingroutes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapHttpRoute(

        name: "DoctorCVPage",
        routeTemplate: "{language}/doctor-cv/{doctorMcr}/{doctorFullName}",
        defaults: new
                              {
                                           controller = "DoctorCVPage",
                                           action = "Index",
                                           doctorMcr = UrlParameter.Optional,
                                           doctorFullName = UrlParameter.Optional,
                                           language = UrlParameter.Optional
                              });

    Here is MyController

                 public class DoctorCVPageController : BaseController
                 {
                                 /// <summary>
                                 /// Gets or sets the message.
                                 /// </summary>
                                 [Category("String Properties")]
                                 public string Message { get; set; }

                                 /// <summary>
                                 /// This is the default Action.
                                /// </summary>
                                public ActionResult Index(string doctorMcr)
                                {
                                         var id =  "";
                                         ViewBag.PageTitleLink = Request.UrlReferrer != null ?                            

                                                                                       Request.UrlReferrer.ToString() : string.Empty;
                                 }
                }

    And then in my view I have a tag.
    <a href="../en/doctor-cv/333333/khoa-Nguyen" />

    After user click on this tag, system should to redirect to

    DoctorCVPage/Index (controller: DoctorCVPage, Action = Index),

    but it can't do this.

    Please helps me know why, thank for all helps.

  2. Pavel Uzunov
    Pavel Uzunov avatar
    7 posts
    Registered:
    13 Sep 2016
    20 Oct 2014
    Link to this post
    Hello David,

    Thank you for contacting us.

    Registration a route is a little different in Sitefinity, because of the specifics of its MVC support. Instead of using the RouteTable directly, you have to use the Bootstrapper class to register routes.

    protected void Application_Start(object sender, EventArgs e)
    {
        Bootstrapper.MVC.MapRoute(
            "Classic",
            "customprefix/{controller}/{action}/{id}",
            new { controller = "Feature", action = "Index", id = (string)null }
        );
    }

    Custom routes will always have to come after Sitefinity routes. 

    Please take a look at this articles - Classic MVC mode and Three MVC modes in Sitefinity, I believe you might find them useful.

    Please let me know if the suggested solution worked on your end.

    Regards,
    Pavel Uzunov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. David
    David avatar
    10 posts
    Registered:
    20 Jul 2014
    22 Oct 2014
    Link to this post

    Hello Pavel Uzunov,

     Thanks so much for your help.

    I have tried your solution in my project such as:
    Bootstrapper.MVC.MapRoute(
                        "Classic",
                        "{language}/doctor-cv/{DoctorMcr}/{DoctorName}",
                        new { controller = "DoctorCVPage", action = "Index", DoctorMcr = (string)null  }
                    );

    I put this code after Sitefinity Route default. And then in the browser I have a link: http://localhost/en/doctor-cv/212308Z/khoa-nguyen.

     It not work for me, but if I add "Index" in above link, it work. So I don't why it happen.
    ex: http://localhost/en/doctor-cv/index/212308Z/khoa-nguyen

  4. Pavel Uzunov
    Pavel Uzunov avatar
    7 posts
    Registered:
    13 Sep 2016
    27 Oct 2014
    Link to this post
    Hi David,

    I tried your route and I saw that the problem can be from the 'DoctorName' parameter, it should be defined to '(string)null' like the previous one. When I tried with this change, it works correctly on my local machine. You can check the attached screenshots.
     
    Regards,
    Pavel Uzunov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  5. David
    David avatar
    10 posts
    Registered:
    20 Jul 2014
    29 Oct 2014 in reply to Pavel Uzunov
    Link to this post

    Hello Pavel Uzunov,

     Thanks for your reply, but maybe my project have something wrong, I will check it carefully.

     Best Regards,

    David

5 posts, 0 answered