ASP.NET MVC 4 Custom View Routing in Sitefinity 7

Posted by Community Admin on 04-Aug-2018 11:01

ASP.NET MVC 4 Custom View Routing in Sitefinity 7

All Replies

Posted by Community Admin on 15-Oct-2014 00:00

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.

Posted by Community Admin on 20-Oct-2014 00:00

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
 

Posted by Community Admin on 22-Oct-2014 00:00

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: localhost/.../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: localhost/.../khoa-nguyen

Posted by Community Admin on 27-Oct-2014 00:00

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
 

Posted by Community Admin on 29-Oct-2014 00:00

Hello Pavel Uzunov,

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

 Best Regards,

David

This thread is closed