Ognjen
Ognjen

Reputation: 1195

HtmlHelper does not contain definition for "Action"

I use <%= Html.Action("ReadXML") %> and have this error:

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Action' and no extension method 'Action' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

How fix it

This is my assemblies:

   <assemblies>
    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  </assemblies>

Upvotes: 0

Views: 9098

Answers (3)

Joel Wiklund
Joel Wiklund

Reputation: 1875

If you want to get the url to an action method inside i.e. a controller you can do:

var baseUrl = "http://localhost:12345";
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
var resource = urlHelper.Action("Home", "Index");
var uri = new Uri($"{baseUrl}{resource}");
Console.WriteLine(uri.ToString()); // ==> http://localhost:12345/Home/Index

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

Action is an extension method contained in the System.Web.Mvc assembly. Make sure you have referenced the following namespaces in your web.config:

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>

The method is declared in the System.Web.Mvc.Html namespace.

Also make sure your project is ASP.NET MVC 2.0 as this method has been added in the 2.0 version.

Upvotes: 1

Oded
Oded

Reputation: 499382

Make sure there is a reference to System.Web.Mvc in your project (add it if not already there).

Then make sure the class that is displaying this problem is importing the namespace - you can do this in a couple of ways:

  • In the code behind add a using System.Web.Mvc; statement at the top.
  • You can also import the namespace directly in the aspx page if needed, by using this statement: <%@ import namespace="System.Web.Mvc"%>

Upvotes: 1

Related Questions