Custom UrlHelper method not being included - c#

I made a custom helper that I was trying to include as available to all pages.
helper:
namespace project.CacheBreaker
{
public static class CacheBreaker
{
public static void CacheBreak(
this UrlHelper url, string contentPath)
{
url.Content(contentPath);
}
}
}
I tried to expose it from web.config:
<system.web>
<pages>
<namespaces>
<add namespace="project.CacheBreaker"/>
</namespaces>
</pages>
</system.web>
I got an error while trying to access it in a view:
<script src="#Url.CacheBreak("~/Scripts/link.js")" type="text/javascript"></script>
which stated:
"Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately."
"Compiler Error Message: CS1061: 'System.Web.Mvc.UrlHelper' does not contain
a definition for 'CacheBreak' and no extension method 'CacheBreak' accepting a
first argument of type 'System.Web.Mvc.UrlHelper' could be found
(are you missing a using directive or an assembly reference?)"
I thought I had taken all the necessary steps, why isn't this custom method available?

But your UrlHelper doesn't return anything. It's void. You can't possibly call it with: #Url.CacheBreak(...). If you want your helper to be used that way it should return an IHtmlString or a string:
public static string CacheBreak(this UrlHelper url, string contentPath)
{
return url.Content(contentPath);
}
Oh and by the way having a helper that just wraps around Url.Content seems kinda useless, coz you could directly write:
<script src="#Url.Content("~/Scripts/link.js")" type="text/javascript"></script>
Also the wbe.config you are talking about in your question seems the wrong wbe.config. You know, there's a ~/web.config and there's also a ~/Views/web.config which are 2 completely different files. If you are using the Razor view engine you should define your namespace in the ~/Views/web.config file:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="project.CacheBreaker"/>
</namespaces>
</pages>
</system.web.webPages.razor>

Related

use namspace within a <script runat="server"> block

I want to use Dictinoary class within a <script runat="server"> block(aspx.cs).Whenever I tried to use that class, it showing The type or namespace name 'Dictionary' could not be found (are you missing a using directive or an assembly reference?) Exception.
How to use Using System.Collections.Generic within a <script runat="server"> block.I don't want to use this namespace in .cs file.
Use the fully qualified name:
System.Collections.Generic.Dictionary<object,object> objName=new System.Collections.Generic.Dictionary<object,object>();
Now, change the parameters, as you wish.
If it still gives you a error, then, there is something else which is throwing that error and not this.
Hi thanks for your answers.
Finally I found solution.
The solution is
you have to insert the required namespace in the web.config file. Then only you can access the namespace within inline server tag, not by using "using" statement. I have added like this:
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace="System.Collections.Generic"/>
<add namespace="System.Data"/>
<add namespace="System.Configuration"/>
<add namespace="System.Data.OracleClient"/>
<add namespace="System.Resources"/>
<add namespace="System.Globalization"/>
</namespaces>
</pages>

Using enum in #Url.Action() routevalues

This question is just a quick one. I have a set of links on a page, and they all actually point to the same controller method - the difference between them is that they each pass a different value to that method so that the subsequent process is slightly different. I happened to already have an enum defined to correspond to the possible values passed, so without thinking, I did this:
#Url.Action("QueryStepTwo", new { type = QueryType.UserRecords })
and was pleasantly surprised to see that nothing gained a red underline. I hit compile and navigated to that page to verify, to be presented with an error message for CS0103: "The name 'QueryType' does not exist in the current context". In the editor, QueryType is syntax-highlighted and IntelliSense provides the options list when I type it.
I'm assuming this is just a case of VS/IntelliSense being just a little bit too smart and knowing things that the actual page parse/render engine can't? Casting the enum to its string or int value doesn't help, so I'm guessing this is to do with the order in which things are executed; more specifically, the enum is out of scope by the time Razor gets to see the page. Is there a way to use an enum in URL helpers like this, especially one that doesn't require the enum to be defined as a member of the view model? I dislike using magic strings all over the place; they're too vulnerable to typos and tomfoolery.
Make sure you fully qualify the namespace where this enum is defined:
#Url.Action("QueryStepTwo", new { type = SomeNamespace.QueryType.UserRecords })
or if you don't want to do that you could also add an #using directive to the top of your Razor view:
#using SomeNamespace
And if you want to do this globally for all Razor views you could add this namespace to the <namespaces> node in ~/Views/web.config (not to be confused with ~/web.config):
<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="SomeNamespace" />
</namespaces>
</pages>
</system.web.webPages.razor>
As far as the Intellisense in Razor views is concerned, this is not something that could be trusted. Hopefully Microsoft will improve it in future versions.

Unable to use extension method within partial view

I am trying to use a string extension method in a partial view. I get the following error:
'string' does not contain a definition for 'TruncateAtCharacter'
Here is the extension method:
namespace PCCMS.Core.Libraries {
public static class Extensions {
public static string TruncateAtCharacter(this string input, int length) {
if (String.IsNullOrEmpty(input) || input.Length < length)
return input;
return string.Format("{0}...", input.Substring(0, length).Trim());
}
}
}
According to this previous question I need to add the namespace to web.config, however I have done this and I still receive the same error message. What's odd though, is that I do get intellisense for the extension method?
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="PCCMS.Core.Libraries.ClientWebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<!-- Other namespaces... -->
<add namespace="PCCMS.Core.Libraries" />
</namespaces>
</pages>
</system.web.webPages.razor>
Can anyone explain why this is?
Thanks
This should work if the namespace declaration is in the system.web.webPages.razor/namespaces element of your root Views directory web.config. If that fails, try using an explicit #using statement at the top of the View without any web.config statements. It 'should' work.
PS Is that ReSharper intellisense or VS? ReSharper explicitly tells me that an #using is required if the web.config entry is not in scope.
Is the error occurring when running in VS debugger, or from a test or production system? Make sure your module containing the extension is properly installed (and updated properly). Also, try running "iisreset" from the command prompt.

Can't find Html.TextBox

I am trying to get an old application that was written using a mvc preview version and have run in the following problem.
<%= Html.TextBox("Register_Name", ViewData.Model.Register.Name, 20, 30, new { _class = "textInput username" })%>
This is the error i get:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'TextBox' and no extension method 'TextBox' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
My question is how do I resolve this? The problem also occurs with the "Html.Password" field.
do you have in start of the page where do you want to use it
<%# Page ..... Inherits="System.Web.Mvc.ViewPage"%>
In your web.config, make sure you have the following:
<compilation>
<assemblies>
<add assembly = "System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace = "System.Web.Mvc" />
<add namespace = "System.Web.Mvc.Html" />
</namespaces>
</pages>

MVC3 Views unable to find added referenced class

My view is having issues finding a class located in one of my references.
This reference is to a dll built outside of the project.
The view always gives error:
The type or namespace name 'Company' could not be found (are you
missing a using directive or an assembly reference)
Here is the controller. No issues there.
using Company.Entities;
public ActionResult Find()
{
Person test = Person.SelectByADDistinguishedName("L*", false);
return View(test);
}
Here is the View. The error occurs in the #Model line.
#model Company.Entities.Person
#{
ViewBag.Title = "Bob";
}
<h2>Find</h2>
My Views/Web.config currently looks like this
<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="Company.Entities" />
</namespaces>
</pages>
</system.web.webPages.razor>
I've checked similar threads like this one but to no avail.
Here is the message on the screen
Line 25: using System.Web.Mvc.Html;
Line 26: using System.Web.Routing;
Line 27: using Company.Entities;
Line 28:
Line 29:
Source File: c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET
Files\root\49c9c7db\1f9dd5c8\App_Web_find.cshtml.a8d08dba.xro6gxci.0.cs
Line: 27
If I strip out any mention of the assembly (out of web.config - no #using statments). I get the same error message when loaded but against this line
public class _Page_Views_Home_Find_cshtml : System.Web.Mvc.WebViewPage<Company.Entities.Person> {
After working it with it for awhile, I ended up solving it.
I had to add the assembly under the assemblies section of the main web.config (not the one under the views)
Sample of the web.config.
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="Company, Version=2.0.31.0, Culture=neutral, PublicKeyToken=df9010405db60e6d"/>
</assemblies>
</compilation>
Thanks for people who gave me suggestions.
Be sure to check your views directory for an additional web.config file. I seem to remember running into a similar problem because I hadn't updated the correct web.config. YMMV.
Adding the reference
before where you use your control, in .cshtml file works. Example:
#model IEnumerable<MyApp.Models.MyModel>
#using System.Web.UI.WebControls;
But you can put it in web.config, under namespaces tag.
This way it is global to all views. Also, there is no need to specify the version:
<namespaces>
<add namespace="System.Web.UI.WebControls" />
</namespaces>

Categories

Resources