How to add WebMatrix default parameters?http://forums.asp.net/t/1588790.aspx/1?How+to+add+WebMatrix+default+parameters+Mon, 06 Sep 2010 00:07:11 -040015887904020264http://forums.asp.net/p/1588790/4020264.aspx/1?How+to+add+WebMatrix+default+parameters+How to add WebMatrix default parameters? <p>That subject may not do this questions justice.</p> <p>I am trying to limit what I write on all my .cshtml pages.</p> <p>For example, on everypage I am @using myDLL and &quot;connecting&quot; to sql server.</p> <p>I write that out like</p> <pre> @using System.myDLL; @{ myDLL dll = new myDLL(); dll.ConnectionString = &quot;blablabla&quot;; }<br></pre> <p>How can I remove the need to do that to every single page? </p> <p>I think I can create a web.config and do some funky stuff... can someone show examples?<br> </p> 2010-08-10T19:34:48-04:004020300http://forums.asp.net/p/1588790/4020300.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>This kind of thing can be achieved using a _start.cshtml or an _init.cshtml file: <a href="http://www.asp.net/webmatrix/tutorials/15-customizing-site-wide-behavior"> http://www.asp.net/webmatrix/tutorials/15-customizing-site-wide-behavior</a></p> <p><br> </p> 2010-08-10T20:02:17-04:004020554http://forums.asp.net/p/1588790/4020554.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Well, I know that and I have tried that... but it doesnt work...</p> <p>error: <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">The name 'myDLL' does not exist in the current context</font></p> 2010-08-10T23:13:34-04:004021031http://forums.asp.net/p/1588790/4021031.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>create a bin folder in root of your website and copy the DLL to the bin folder. then you can access the DLL from _init.cshtml.</p> <p><br> </p> <p><br> </p> <pre><br><br><br></pre> <p><br> </p> <p><br> </p> 2010-08-11T06:46:09-04:004022350http://forums.asp.net/p/1588790/4022350.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>It's already in a bin folder</p> <p>and again... I already tried the _init.cshtml as well as the _start.cshtml<br> </p> <p><br> </p> 2010-08-11T17:20:08-04:004022424http://forums.asp.net/p/1588790/4022424.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Here is my _init.cshtml</p> <p><pre class="prettyprint">@using System.CapTool.Server.DAL; @using System.Configuration; @{ DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings[&quot;ConnectionString&quot;]; Exception ex = null; }</pre><br> <br></p><p>Here is my other page ... data.cshtml</p><p><br><pre class="prettyprint">@{ var data= mydll.getfunction(year, username, out ex); Response.Write(data); }</pre><br> doesnt find it...</p> <p>A couple things I've looked at: 1. The connection string is definetly a working string. If I DO NOT seperate the _init.cshtml stuff from all the pages, it works. Sooo the functions are valid, it works without the _init.cshtml, and all i'm seeing in my error is cannot find reference to mydll</p> <p>...</p> <p><br> </p> <p>is there any way around having to write down the connection string, object instance, and my exeption on EVERY single page?<br> </p> 2010-08-11T18:18:07-04:004022700http://forums.asp.net/p/1588790/4022700.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Unfortunately variables aren't global.&nbsp; They're scoped to the page you're working on.</p> <p>For now you have to build your own class with a method that sets up your class and call that from every page.</p> <p>Drop a file called DALHelpers.cs into the App_Code in the root of your app.&nbsp; Put this in the file...</p> <p><pre class="prettyprint">public static class DALHelpers { public static DAL CreateDAL() { DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings[&quot;ConnectionString&quot;]; return mydll; } }</pre></p><p> From any page now you can do this...</p><p><pre class="prettyprint">@{ var mydll = DALHelpers.CreateDAL(); var data= mydll.getfunction(year, username, out ex); Response.Write(data); }</pre></p> <p>Not ideal, but should work for you.&nbsp; In the next release we'll allow you to put an @functions {} block into a file in App_Code and the methods will be globally accessible to any page.&nbsp; That should simplify it a bit.<br> </p> 2010-08-11T22:18:10-04:004022712http://forums.asp.net/p/1588790/4022712.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Darn...</p> <p>If I can't do global variable, like I have above, can I atleast do something funky to the web.config to take care of my problem? </p> <p>Kind of similar to how Webforms handles the option to globally register controls on every single page user controls are used in.</p> <p><pre class="prettyprint">&lt;controls&gt; &lt;add tagPrefix=&quot;my&quot; tagName=&quot;menu&quot; src=&quot;~/MenuControl.ascx&quot; /&gt; &lt;/controls &gt;</pre><br> Can that apply to razor? <br> </p> 2010-08-11T22:35:26-04:004022720http://forums.asp.net/p/1588790/4022720.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>No, that is for web forms only.</p> <p>One of our devs reminded me that you could also stuff what you need in Context.Items.&nbsp; So maybe something like this...<br> <br> _init.cshtml<br> <pre class="prettyprint">@{ DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings[&quot;ConnectionString&quot;]; Context.Items[&quot;mydll&quot;] = mydll; }</pre></p><p><br> Then in your page...</p><p><pre class="prettyprint">@{ var mydll = Context.Items["mydll"] as DAL; var data= mydll.getfunction(year, username, out ex); Response.Write(data); }</pre><br> Still suboptimal, but both ways should work fine.<br> </p> 2010-08-11T22:45:26-04:004022734http://forums.asp.net/p/1588790/4022734.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>I like that approach...</p> <p>Will this allow me to reference my dll once from the _init.cshtml?</p> <p>Meaning, I only need to use @using mydll; once? and in the _init.cshtml? and not in the page.cshtml so that those three lines are in fact the ONLY guys I would use across the application?<br> </p> 2010-08-11T23:03:09-04:004022742http://forums.asp.net/p/1588790/4022742.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Unfortunately, no.&nbsp; The &quot;as DAL&quot; in there will blow up without a using at the top of the page.&nbsp; The first approach I suggested should work without a using though.<br> </p> 2010-08-11T23:17:44-04:004060137http://forums.asp.net/p/1588790/4060137.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Hey... Interesting thing I stumbled on...</p> <p>I created a Global.cs file and placed it in my app_code directory. I assigned my instances and variables as public static members...</p> <p><br> </p> <pre class="prettyprint">using System; using System.MyDLL.Server.DAL; using System.Configuration; public static class Global { public static Exception ex = null; public static DAL dal = new DAL(); public static string ConnectionString = ConfigurationManager.ConnectionStrings[&quot;DB&quot;].ConnectionString; public static void SetConnectionString() { dal.ConnectionString = ConnectionString; } }</pre><p><br> For the sake of keeping my connection string available on every page I called the SetConnectionString (above) function,</p><p></p><pre class="prettyprint">@{ Global.SetConnectionString(); }</pre><p>Becuase of the public static members in Global.cs I can call my instanced dal without creating a new instance of dal (this was done in Global.cs) </p><p><pre class="prettyprint">Global.dal.object; void myfunction(Global.something, out Global.ex);</pre> <p></p> <p>In conclusion, </p> <p>By first created a public static class I can use it throughout my project...</p> <p><br> </p> <p><br> </p> <p><br> <br> </p> 2010-09-03T14:26:53-04:004060338http://forums.asp.net/p/1588790/4060338.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>I am almost completely sure that won't scale and you'll have issues when requests start coming in fast and each request is hitting the same variable.&nbsp; Your object should really be instantiated per request (stuffed in Context.Items or something that lives for the life of the request)...for now.</p> <p>We're finishing up new simplified state management for the next release that will make what you're trying to do, much easier.&nbsp; Basically, you'll be able to say Page.dal = new DAL(); and set all your initial properties you need in _Init.cshtml, then in any web page you can access Page.dal.&nbsp; Simple.&nbsp; :)<br> </p> 2010-09-03T16:32:12-04:004062344http://forums.asp.net/p/1588790/4062344.aspx/1?Re+How+to+add+WebMatrix+default+parameters+Re: How to add WebMatrix default parameters? <p>Darn, I thought I found something cool &gt;&lt;</p> <p>What kind of issues will accur when requests come in faster?</p> <p>I can revert back to the Context.Items as you mentioned earlier and thats fine...</p> <p>However.... When does the next release come out?? heheh (=&nbsp; !?!?!?<br> </p> 2010-09-06T00:07:11-04:00