I've got the following problem. Our course registration system has pages for 5 schools. Each school has it's own (sub)domain which is like this:
school.project.domain.nl
The general landing page has links to all kinds of courses from different schools. These links should be including the school part from the link above.
Users go to
project.domain.nl
So the url does not contain the subdomain school at that point.
I need to include the subdomain as part of the url that is generated. Below is the code that has the problem. The school is available in the string variable course.school so how to I add the subdomain school to the link project.domain.nl?
<a href="@Url.Action("Details", "Courses", new { course.Id })">@course.Title</a>
Since Url.Action()
will by default* generate a link in the form of /controller/action/whatever
, so you can just prefix it with the domain of your choice. For example:
<a href="@string.Format(
"http://school.project.domain.nl",
Url.Action(" Details", "Courses" , new { Model.Content.Id }))">@Model.Content</a>
Of course, you probably want to put this in a helper method. For example:
public static class UrlHelperExtensions
{
public static string SchoolAction(this UrlHelper helper, string school,
string actionName, string controllerName, object routeValues)
{
var url = $"http://{school}.project.domain.nl";
var action = helper.Action(actionName, controllerName, routeValues);
return $"{url}{action}";
}
}
And now you can do this:
<a href="@Url.SchoolAction("schoolname", "Details", "Courses",
new { Model.Content.Id }))">@Model.Content</a>
* You can make it include the domain, but that isn't happening here.