0

Today, I found a very nice feature giving me the ability to add cshtml helper. To be able to do that, I must put cshtml file in a folder named 'App_Code' and then adding into a code that looks like that:

@using System.Security.Policy

@helper jQuery(){
   if (System.Diagnostics.Debugger.IsAttached)
   {
        <script src="@Url.Content("~/Scripts/JQuery/jquery-1.7.2.js")" type="text/javascript"></script>
   }
   else
   {
        <script src="@Url.Content("~/Scripts/JQuery/jquery-1.7.2.min.js")" type="text/javascript"></script>
   }
}

The problem is immediatly after adding this folder with just one file, when I try to access page in browser, I receive a server error.

"NetworkError: 500 Internal Server Error - http://localhost/[Port]...

I already have code into other folders of my project.

Must I have to move all my code into the 'App_Code' folder or is there any trick to keep my existing structure but with this new 'App_Code' folder?

Thank you.

1 Answer 1

2

You can't use helpers within the helper so they are not as helpful as you might have initially thought :-)

Things like Html and Url don't exist within those helper functions. So if you need to use them, you will have to pass them as parameters:

@using System.Web.Mvc
@using System.Security.Policy

@helper jQuery(UrlHelper urlHelper)
{
   if (System.Diagnostics.Debugger.IsAttached)
   {
        <script src="@urlHelper.Content("~/Scripts/JQuery/jquery-1.7.2.js")" type="text/javascript"></script>
   }
   else
   {
        <script src="@urlHelper.Content("~/Scripts/JQuery/jquery-1.7.2.min.js")" type="text/javascript"></script>
   }
}

and when calling pass the instance of the current UrlHelper to it:

@Foo.jQuery(Url)
Sign up to request clarification or add additional context in comments.

4 Comments

I search on the web and found a guy saying this: "All MVC project by default are Web Application Projects (WAP) instead of Web Site projects. This means that there's no need for an App_Code folder since WAPs always get compiled anyway. That means that all *.cs files in your project will get compiled, as opposed to Web Site projects where only *.cs files in your App_Code folder would get compiled." If I understand correctly, I can't have both code in a App_Code folder and in other folder in my project.
You can have App_Code with Razor. Don't worry. Just make sure that this folder doesn't contain any .cs files. Only Razor stuff: .cshtml.
Ok, I just figure out what you said! The reason why my browser returns an error was because my project don't compile my code. Your a genius guy !!! Thank you very much.
Just for fun, do you have a job Darin? How can you have 341k while working 8 hours a day? Sorry, .... how can you have 341k in all cases except having 250 IQ and searching on the web 24 hours a day? Thank you again for saving my a lot of time in my life ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.