@using System.CapTool.Server.DAL;
@using System.Configuration;
@{
DAL mydll = new DAL();
mydll.ConnectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
Exception ex = null;
}
Here is my other page ... data.cshtml
@{
var data= mydll.getfunction(year, username, out ex);
Response.Write(data);
}
doesnt find it...
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
...
is there any way around having to write down the connection string, object instance, and my exeption on EVERY single page?
Unfortunately variables aren't global. They're scoped to the page you're working on.
For now you have to build your own class with a method that sets up your class and call that from every page.
Drop a file called DALHelpers.cs into the App_Code in the root of your app. Put this in the file...
public static class DALHelpers {
public static DAL CreateDAL() {
DAL mydll = new DAL();
mydll.ConnectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
return mydll;
}
}
From any page now you can do this...
@{
var mydll = DALHelpers.CreateDAL();
var data= mydll.getfunction(year, username, out ex);
Response.Write(data);
}
Not ideal, but should work for you. 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. That should simplify it a bit.
Will this allow me to reference my dll once from the _init.cshtml?
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?
Erik5388
Member
81 Points
84 Posts
How to add WebMatrix default parameters?
Aug 10, 2010 07:34 PM|LINK
That subject may not do this questions justice.
I am trying to limit what I write on all my .cshtml pages.
For example, on everypage I am @using myDLL and "connecting" to sql server.
I write that out like
@using System.myDLL; @{ myDLL dll = new myDLL(); dll.ConnectionString = "blablabla"; }
How can I remove the need to do that to every single page?
I think I can create a web.config and do some funky stuff... can someone show examples?
web.config Database DLL cshtml connectionstring c#
zettersten.com
client side dev
canvas/html5/js/css
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: How to add WebMatrix default parameters?
Aug 10, 2010 08:02 PM|LINK
This kind of thing can be achieved using a _start.cshtml or an _init.cshtml file: http://www.asp.net/webmatrix/tutorials/15-customizing-site-wide-behavior
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Erik5388
Member
81 Points
84 Posts
Re: How to add WebMatrix default parameters?
Aug 10, 2010 11:13 PM|LINK
Well, I know that and I have tried that... but it doesnt work...
error: The name 'myDLL' does not exist in the current context
zettersten.com
client side dev
canvas/html5/js/css
tanatrajan
Participant
1784 Points
370 Posts
Re: How to add WebMatrix default parameters?
Aug 11, 2010 06:46 AM|LINK
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.
Erik5388
Member
81 Points
84 Posts
Re: How to add WebMatrix default parameters?
Aug 11, 2010 05:20 PM|LINK
It's already in a bin folder
and again... I already tried the _init.cshtml as well as the _start.cshtml
zettersten.com
client side dev
canvas/html5/js/css
Erik5388
Member
81 Points
84 Posts
Re: How to add WebMatrix default parameters?
Aug 11, 2010 06:18 PM|LINK
Here is my _init.cshtml
@using System.CapTool.Server.DAL; @using System.Configuration; @{ DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"]; Exception ex = null; }Here is my other page ... data.cshtml
@{ var data= mydll.getfunction(year, username, out ex); Response.Write(data); }doesnt find it...
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
...
is there any way around having to write down the connection string, object instance, and my exeption on EVERY single page?
zettersten.com
client side dev
canvas/html5/js/css
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: How to add WebMatrix default parameters?
Aug 11, 2010 10:18 PM|LINK
Unfortunately variables aren't global. They're scoped to the page you're working on.
For now you have to build your own class with a method that sets up your class and call that from every page.
Drop a file called DALHelpers.cs into the App_Code in the root of your app. Put this in the file...
public static class DALHelpers { public static DAL CreateDAL() { DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"]; return mydll; } }From any page now you can do this...
@{ var mydll = DALHelpers.CreateDAL(); var data= mydll.getfunction(year, username, out ex); Response.Write(data); }Not ideal, but should work for you. 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. That should simplify it a bit.
ASP.NET PM
http://about.me/erikporter
Erik5388
Member
81 Points
84 Posts
Re: How to add WebMatrix default parameters?
Aug 11, 2010 10:35 PM|LINK
Darn...
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?
Kind of similar to how Webforms handles the option to globally register controls on every single page user controls are used in.
<controls> <add tagPrefix="my" tagName="menu" src="~/MenuControl.ascx" /> </controls >Can that apply to razor?
zettersten.com
client side dev
canvas/html5/js/css
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: How to add WebMatrix default parameters?
Aug 11, 2010 10:45 PM|LINK
No, that is for web forms only.
One of our devs reminded me that you could also stuff what you need in Context.Items. So maybe something like this...
_init.cshtml
@{ DAL mydll = new DAL(); mydll.ConnectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"]; Context.Items["mydll"] = mydll; }Then in your page...
@{ var mydll = Context.Items["mydll"] as DAL; var data= mydll.getfunction(year, username, out ex); Response.Write(data); }Still suboptimal, but both ways should work fine.
ASP.NET PM
http://about.me/erikporter
Erik5388
Member
81 Points
84 Posts
Re: How to add WebMatrix default parameters?
Aug 11, 2010 11:03 PM|LINK
I like that approach...
Will this allow me to reference my dll once from the _init.cshtml?
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?
zettersten.com
client side dev
canvas/html5/js/css