Reputation: 135
We have multiple web applications deployed on the same server needed to communicate to each other. For example, there are 2 applications deployed in wwwroot folder of IIS: Foo and Bar.
The problem is: from a FooView.cshtml view in the Foo web application, I need to call to MyAction of the Test controller from the Bar web application.
For now we have to hard-code the url like this:
<a href="\Bar\Test\MyAction?id=100">Test action</a>
How to define it in the route config so that we can use method like Url.Action or Url.Route to call the action? Thanks in advance.
Upvotes: 3
Views: 564
Reputation: 23680
It doesn't make sense to use @Url.Action
or @Html.RouteLink
if its to create a link for an action that exists outside of your application.
The helpers make use of the routing information that exists within your application and produces relative links accordingly. The route configuration explicitly restricts external URLs fro being defined within a route.
You could easily create your own HTML helper to link to it if you do it frequently:
namespace System.Web.Mvc.Html
{
public const string ExternalAppName = "externalapp";
public static class ExternalHelpers
{
public static MvcHtmlString ExternalLink(this HtmlHelper htmlHelper, string externalPath, string linkText)
{
var tb = new TagBuilder("a");
tb.MergeAttribute("href", Path.Combine("/" + ExternalAppName + externalPath));
tb.SetInnerText(linkText);
return new MvcHtmlString(tb.ToString());
}
}
// Accepts a controller and action
public static MvcHtmlString ExternalLink(this HtmlHelper htmlHelper, string controller, string action, int id, string linkText)
{
var tb = new TagBuilder("a");
tb.MergeAttribute("href", Path.Combine("/" + ExternalAppName + "/", controller + "/" + id.ToString()));
tb.SetInnerText(linkText);
return new MvcHtmlString(tb.ToString());
}
}
View:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.ExternalLink("/home/index", "Click to go to external app!")
Upvotes: 1