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

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>

Related

Implicit namespace and service reference

I've got a service reference of type ServiceReference in the namespace MyNamespace, containing some data classes' definitions. That leads to the following syntax in my CSHTML file.
#foreach(MyNamespace.ServiceReference.MyDataType blopp in ViewBag.Blopps) ...
This is less than convenient and I'd like to be able to go like this, instead.
#foreach(MyDataType blopp in ViewBag.Blopps) ...
In a CS file, I'd apply using for this but what's recommended approach for the CSHTML file?
EDIT
Of course, besides #using MyNamespace.ServiceReference; :)
Use:
#using MyNamespace.ServiceReference
EDIT:
Since in your edited question you specified, besides #using, you can add the references in web.config file but they will be available for all the views.
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<!-- Add following with others -->
<add namespace="MyNamespace.ServiceReference" />
</namespaces>
</pages>
</system.web.webPages.razor>

Razor to talk to class function?

I am trying to add social networking buttons to my site and have build a socialNetworkingHelper class.
I would like to know what i need to do to get the view to see the helper class.
I followed the following link to get where i am now.
http://www.advancesharp.com/Blog/1033/add-facebook-twitter-and-google-plus-in-c-mvc-application
I am using MVC 4 and have placed this div in my layout page in my footer.
<div>
#Html.SocialLinkButtons(Model.Title, Request.Url.ToString())
</div>
The SocialLinkButtons text is underlined in red with the following error message.
'System.Web.Mvc.HtmlHelper' does not contain a definition for
'SocialLinkButtons' and no extension method 'SocialLinkButtons'
accepting a first argument of type
'System.Web.Mvc.HtmlHelper' could be found (are you missing
a using directive or an assembly reference?)
Is there somthing i must add to my view to allow the #html to call my socialLinkButtons?
Or is there something i need to add to my class to allow this?
I have tried placing my class in the contoller folder and the models folder and just plain in the whole project. Where should this helper class be kept?
To see the class please follow the link placed above.
Edit:
Following the answers below the SocialLinkButtons text is no longer underlined but when i run my code i get the following error.
'System.Web.Mvc.HtmlHelper' has no applicable method named
'SocialLinkButtons' but appears to have an extension method by that
name. Extension methods cannot be dynamically dispatched. Consider
casting the dynamic arguments or calling the extension method without
the extension method syntax.
You shoud to put code from link in any namespace:
namespace Helpers {
public static class SocialNetworkingHelper {
...
}
}
and add using directive in your view:
#using Helpers
Probably you extension method SocialLinkButtons is not visible in the view.
You should add #using MyNamespace directive in the view where MyNamespace is actual namespace of your extension method SocialLinkButtons or youcan achieve the same thing via web.config by adding this namespace to
<system.web.webPages.razor>
<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="System.Collections" />
<add namespace="MyNamespace" />
</namespaces>
</pages>
</system.web.webPages.razor>

Custom UrlHelper method not being included

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>

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.

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>

Categories

Resources