0

I am trying to Invoke action result from an anchor tag,

I am building html into C# , its not in view and injecting it into view using html helper @Html.Raw(Model.HTML) like this.

HTML is

html.AppendLine("<td><a href='Home/DeleteFile?ID="+ dt.Rows[i]["ID"].ToString() + "'><i style='color:#e90029' class='fa fa-times fa-lg' aria-hidden='true'></i></a></td>");

The Problem is when i click first time on this anchor tag to delete the record it works by invoking address

http://WebsiteAlias/Home/DeleteFile?ID=2

But after that when i hover to another record to delete them it show URL to

http://WebsiteAlias/Home/Home/DeleteFile?ID=2

It add Another /Home in url ,

Why it is happening ?

I cant use @Url.Action("Action","Controller") cause HTML is coming from c#

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Tanwer
  • 1,503
  • 8
  • 26
  • 41

1 Answers1

2

If the C# code that builds the HTML has access to the HttpContext, you can create an UrlHelper like this:

using System.Web;

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
string url = urlHelper.Action("DeleteFile", "Home", new { ID = dt.Rows[i]["ID"].ToString() });
return url;

Edit: if you only have access to HttpContextBase, you can get the current HttpContext like this:

HttpContextBase contextBase;
HttpContext httpContext = contextBase.ApplicationInstance.Context;
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36