ASP.NET MVC 4 Custom View Routing in Sitefinity 7
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.
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
);
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
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
Hello Pavel Uzunov,
Thanks for your reply, but maybe my project have something wrong, I will check it carefully.
Best Regards,
David