I've created an extension method:
namespace MyComp.Web.MVC.Html
{
public static class LinkExtensions
{
public static MvcHtmlString ActionImageLink(this HtmlHelper htmlHelper, string linkText, string imageSource, string actionName)
{
...
}
}
}
I've referenced the assembly from my mvc app, and I've tried importing the namespace in my view:
<%# Import Namespace="MyComp.Web.Mvc.Html" %>
and I've also added it to the web config file:
<pages>
<controls>
...
</controls>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
<add namespace="MyComp.Web.Mvc.Html"/>
</namespaces>
</pages>
In My view if I try to access Html.ActionImageLink I get an error saying that System.Web.Mvc.HtmlHelper does not contain a definition for ActionImageLink accepting a first argument type of System.Web.Mvc.HtmlHelper. I don't see any of the ActionLink extension methods for System.Web.Mvc.HtmlHelper, only for System.Web.Mvc.HtmlHelper, so how does it work for the .net framework, and not for me?
Notice the difference in the case of your namespace when declaring and when importing.
namespace MyComp.Web.MVC.Html
{
}
<%# Import Namespace="MyComp.Web.Mvc.Html" %>
<add namespace="MyComp.Web.Mvc.Html"/>
Namespaces are case-sensitive!
You must add the namespace in the web.config but in the one inside the Views Folder
Try shutting down Visual Studio and opening your Solution again. When things start acting weird, some times this helps.
Close and reopen Visual Studio did the trick!!
Does the VS intellisense autocompletes your extension method? Does it autocompletes standard MVC helpers methods? If not then the view complilation error occured. Make sure you have the proper "Inherits" attribute value in Page tag at the beginning of the view. If you use strongly typed views make sure the "strong type" exists and compiles.
Do you define the extension method in the same project where the view is defined? If not you have to add the reference in the mvc project. Finally check if the assembly with the extension method (MyComp.Web.Mvc.Html.dll?) is in the Bin folder of the application
Try to add the namespace declaration to the pages/namespaces section of the web.config file placed in your Views folder in MVC project (not the main project web.config file).
One of the reasons may be you are returning a MvcHtmlString and not a string.
Include the namespace for class MvcHtmlString. See if it helps.
Related
I'm Following this article http://timdwilson.github.io/typeahead-mvc-model/
and in the (e) part of this article it uses #Html.AutocompleteFor but my project dosen't recognize it. it has the Error : Html helper dose not contain a definition for autocompleteFor
Appreciate any idea to help. thanks.
#Html.AutocompleteFor() is an extension method for the HtmlHelper class.
Somewhere in the code you have downloaded, you will find a method that will have a signature similar to
public static MvcHtmlString AutocompleteFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper ...... )
Looking at the images in your link, its most likely in the /Controllers/HtmlHelper.cs file. At the top of that file, beneath any using statements, you will find the name of the namespace
namespace xxxx
{
public static class .....
In the view, add (where xxxx is the name of the namespace)
#using xxxx
If you want to make this available in all your views, you can add it to web.config.csfile so that you do not need the using statement in the view
<system.web>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
....
<add namespace="xxxx" /> // add the namespace here
</namespaces>
Is it possible to call a function in Global.asax from a class libray project which is present in the same solution?
To access the app settings in web.config from your code, just add the following code:
using System.Configuration;
...
string appVersion = ConfigurationManager.AppSettings["AppVersion"];
It assumes, your web.config contains the following section:
<appSettings>
<add key="AppVersion" value="1.0.1"/>
</appSettings>
If the System.Configuration namespace is not known, then you have to add a reference to System.Configuration.
I can't get intellisense for my own html helpers. My CustomHtmlHelpers.cs looks like this:
using System.Web.Mvc;
using System.Text;
using System.Web;
namespace laget.Web.Helpers
{
public static class CustomHtmlHelpers
{
//MY HELPERS
}
}
and in my Web.config:
<pages>
<namespaces>
<add namespace="laget.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="System.Web.Helpers" />
</namespaces>
</pages>
If I put <#using laget.Web.Helpers> in my view, I get the intellisense issue fixed.
Should it not be enough with the code in Web.config?
Sometimes it doesn't seem to work right away. Try closing the .cshtml file, and re-opening it. Then if that doesn't work, try restarting Visual Studio. Also make sure you actually compiled your project, intellisense won't work with non-compiled helpers.
I'm pretty sure that you're not editing the correct Web.config file.
You need to add your namespace to the one in your Views directory.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="laget.Web.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
You actually don't need to restart Visual Studio in most cases. All you need to do is close the .cshtml file and reopen it!
It needs it on the local page. I'm pretty sure this has to do with Namespace resolution. It isn't exactly sure what you are referring to without the local using statement.
I ran into this today as well. Sometimes just closing the Razor view's window in Visual Studio and re-opening it will do the trick without having to do a full Visual Studio restart.
I tried to solve an issue like this one yesterday. I had e pre-compiled dll (project name ie: MyHtmlHelpers) containing helpers and lot of other classes.
I had the assembly referenced in the web project and the all "standard"-helpers showed up in intellisense but, even though I added the namespace to both web.config in the root and in the views-folder nothing worked. When running the project helpers works, but not in intellisense.
I added a new class and wrote a new html helper inside the web project, added the namespace to web.config. And that worked.
After some hours add tried my last card, adding the MyHtmlHelpers-project to the same solution as my webproject. That did the trick. I diden't change anything in the configs just added the project to the same solution and changed the reference to point at the project insted of the compiled dll.
Isen't that strange? A VS-bug?
I found that i was adding the reference to the wrong web.config. It's not the main config but the web.config in the views directory...
So now I will show you the steps
1.Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)
2.Add the MVC (5.0) nuget package (
right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
3.Close any and all open .cshtml files
4.Right click project -> Properties -> Build -> change Output path to your project “bin/”
5.Add the following minimal Web.config to the root of your class library project
( the web config file is solely needed for intellisense. Configuration (via Web.config)
should be done in the WebApplication hosting your ClassLibrary assembly)
6.Clean and Build the solution.
7.Open cshtml file and try now :)
I found that if it still doesn't work, you may need to go to the properties of the custom class and change the build action from "content" to "compile". That resolved it for me.
I try all of this solutions, one more thing which i didnt find is that in root web.config i must change webpages:Version from 2.0.0.0 to 3.0.0.0.
Open and close all .cshtml files and it's worked.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
This question already has an answer here:
Generic Inherited ViewPage<> and new Property
(1 answer)
Closed 2 years ago.
In my MVC apps I normally declare a base view type that all of my views inherit from. I get a parser error when I specify Inherits="MyView" in my Page declaration, but not if I specify Inherits="MyApp.Web.Views.MyView".
Strangely enough, it works fine if I specify a strongly typed view name: Inherits="MyView<T> (where T is any valid type).
Why can I specify a strongly typed view without the full type name, but not a generic view?
My base view class is declared like this:
namespace MyApp.Web.Views {
public class MyView : MyView<object> {
}
public class MyView<TModel> : ViewPage<TModel> where TModel : class {
}
}
UPDATE: Note that I do import MyApp.Web.Views via web.config. If I did not, then the strongly typed approach (Inherits="MyView<T>") wouldn't work either.
From another post: Here's the underlying issue: the ASP.NET page parser does not support generics as a page type
Read more here- Generic Inherited ViewPage<> and new Property
You can add your namespace to the <namespaces> element of your web.config file then you should be able to use Inherits="MyView"
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="MyApp.Web.Views" />
</namespaces>
</pages>
Today I spent a good three hours trying to convert the project MvcContrib.Samples.InputBuilders, included in MVC Contrib to make it work with Spark View Engine, but so far was unable to do so.
Does anybody have a clue why these two just won't get along?
Changes I've made
InputForm.spark:
<viewdata model="SampleInput" />
!{Html.InputForm()}
Global.asax.cs:
...
protected void Application_Start() {
RegisterRoutes(RouteTable.Routes);
InputBuilder.BootStrap();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new SparkViewFactory());
}
Web.config:
...
<spark>
<compilation debug="true"/>
<pages automaticEncoding="true">
<namespaces>
<add namespace="System"/>
<add namespace="System.Collections.Generic"/>
<add namespace="System.Linq"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="MvcContrib.UI.InputBuilder"/>
<add namespace="MvcContrib.UI.InputBuilder.Views"/>
<add namespace="Web.Models"/>
</namespaces>
</pages>
</spark>
(I copied the last three namespaces from the sample project.)
Errors I'm getting
Depending on the order in which I setup Spark/InputBuilder in Global.asax.cs, I get two different exceptions.
If I first setup InputBuilder, then Spark (code shown above):
error CS1061:
'System.Web.Mvc.HtmlHelper' does not
contain a definition for 'InputForm'
and no extension method 'InputForm'
accepting a first argument of type
'System.Web.Mvc.HtmlHelper' could be
found (are you missing a using
directive or an assembly reference?)
If I first setup Spark, then InputBuilder:
The view 'InputForm' or its master
could not be found. The following
locations were searched:
~/Views/Home/InputForm.aspx
~/Views/Shared/InputForm.aspx
~/Views/InputBuilders/InputForm.aspx
~/Views/Home/InputForm.ascx
~/Views/Shared/InputForm.ascx
Change input.spark to:
<use namespace="MvcContrib.UI.InputBuilder"/>
<use namespace="MvcContrib.UI.InputBuilder.Views"/>
<add namespace="Web.Models"/>
<viewdata model="SampleInput" />
!{Html.InputForm()}
Adding to web.config doesn't work in Spark. You can use _global.spark instead.
There is also another problem. In stable Spark, SparkView Html property is of type HtmlHelper, not HtmlHelper<TModel>. Html.InputForm() function works only for HtmlHelper<TModel>, so you will have to download Spark source and use development build, because it was changed recently. You can also download stable sources and change it yourself. Here is some info:
http://groups.google.com/group/spark-dev/browse_thread/thread/618bd44a94368d22/f7df24e52924f4dc?show_docid=f7df24e52924f4dc
Your on the right track with setting up the input builders and then the spark view engine.
You can see from the source file from mvccontrib that you need this namespace defined MvcContrib.UI.InputBuilder.Views for your view to reference the input builders HtmlHelper Extentions.
http://github.com/mvccontrib/MvcContrib/blob/master/src/MVCContrib/UI/InputBuilder/Views/HtmlExtensions.cs
I wrote the input builders but I do not know enough about the spark view engine to know why it is not resolving the reference to the extention methods for the input builders.