Reputation: 377
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 routing
routes.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/${DoctorMcr}/${DoctorName}"/>
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.
Upvotes: 1
Views: 949
Reputation:
Please follow this way, I think it can help you handle that thing.
protected override void HandleUnknownAction(string actionName)
{
UrlDetail = Request.Url != null ? Request.Url.ToString() : string.Empty;
Response.StatusCode = (int)HttpStatusCode.OK;
this.ActionInvoker.InvokeAction(this.ControllerContext, "Index");
return;
}
public ActionResult Index()
{
return;
}
Hope this way can help you.
Upvotes: 3
Reputation: 4283
It's not working because any URL in Sitefinity will be routed to a page. Your widget is not a page, but on the page. What you have to do is override the HandleUnknownAction
method and parse the URL there, then take the appropriate action.
For example
protected override void HandleUnknownAction( string actionName )
{
if (!RouteHelper.GetUrlParametersResolved())
{
this.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
else
{
this.Response.StatusCode = (int)HttpStatusCode.OK;
List<string> segments = RouteHelper.SplitUrlToPathSegmentStrings( Request.Url.LocalPath, true )
.Skip(1)
.ToList();
ViewBag.PageTitleLink = String.IsNullOrEmpty(segments.ElementAt(2)):String.Empty:segments.ElementAt(2);
var lang = segments.ElementAt(0);
// etc
View( "Index", model ).ExecuteResult( this.ControllerContext );
}
}
This relies on the position of the url segments being consistent, but it works well otherwise. This is the only way I have found to deal with the requirements you have. Also notice that this is a Void method, so you have to execute the view, as shown.
Upvotes: 2