I have several internals object in an API.
I can access them from the code behind but I can't access them from the asp page like :
<% API.InternalObject.Method %>
Is there a way to set the namespace like namespace project {} but for the ASPx file ?
You can use [assembly: InternalsVisibleTo("Other.Assembly")] attribute.
But I would first think if you really need it.
I've used it in the past to make it possible to test internal classes in my test projects.
Related
I want to create classes that are separate files (not code within he aspx.cs) file, and I do not want to put them in the APP_CODE directory.
I want the separate class files to reside in the same directory as the main code behind file (aspx.cs).
Each class will perform a function.
I may be using these classes improperly so correct my terminology if I am wrong.
What I want is for many of my functions (methods) to be code in files outside my code behind file and simply called from withing the code behind file. I believe I do this by creating classes. Sorry if I am wrong I am quite a bit rusty.
Each class (or whatever it is called) will be named according to its function.
I am so ignorant of how to do this, I really need a tutorial. I cannot find any, or any other answers that address exactly what I want to do. I believe this is called an "outer class". Where as an "inner class" is written inside of another class.
I know how to write a class file, I just don't know how to access it from the code behind file aspx.cs
I don't think the file path matters, but you need to use the namespace right. If you have the Question class defined within the TestProject.Pages namespace in your project, like this:
namespace TestProject.Pages
{
public class Question
{
}
}
Then you add this line to your aspx.cs file, which lets that code see the namespace you used above:
using TestProject.Pages
and you should be able to access the Question class.
It's simple!
// you can access classes that are in different folders. by mention the folder in which class resides then put '.' then class name.
e.g :
myfolder.myclass cls = new myfolder.myclass();
I have a complex class for configuration, which holds all the configuration data, and I'm reading this class in a separate class that exists in the business class library. In my web project, I am reading this in a pagebase class - which all my aspx pages inherit. I have the class as a property in pagebase, and I can access it in my aspx pages fine. But how do I access this in the usercontrols? Is there a global way to keep this class in memory (I can't have it as static, because its different per user) and I'd rather not use sessions. I was thinking there is a way to have it as part of a global properties somewhere, so I can always use it anywhere in the web project.
If its on all your pages you can just cast the page object to your base page and access the property.
inside a usercontrol.
((MyBasePage)this.Page).MyConfigObject
You can put it in HttpContext.Current.Items dictionary around page creation time. It will be avaialbe during lifetime of that request.
Note: Tey to find other ways to achieve whatever you want to achieve without global/per-request state...
Ayup
I might be asking a dumb question, but I have a client for whom I need to build many websites (10+) (asp.net 3.5) which will all the pages on each site will have the same codebehind, but the sites will launching in different regions and whilst following the same template, will have different content.
I have built and launched site 1, and sites 2, 3 & 4 is nearly live, but it occurs to me that as all the sites are basically the same, the code is going to get more complicated to update as it will be duplicated, so if I need to do a bug fix on one site, I'll need to do the fix on all websites (and this is going to get complicated.)
I was wondering if it possible to somehow create a class library of all the current aspx.cs files, reference this dll in each website and then inherit these classes into the .aspx.cs files. So default.aspx in each site would still have a CodeFile of "Default.aspx.cs", but Default.aspx.cs would inherit the corresponing class from the dll:
using WebPagesClass;
public partial class _Default : WebPagesClass._Default
{ }
The reason for doing it like this is that if I need to change any code on a specific website (for minor changes in languages for instance), I can override the page functions and change the parts required. For all other pages which have not cha, I can just copy from a single website.
Is this vaguely possible? If not anyone one got any killer suggestions of how to manage so many websites from a single codebase?
Cheers
T
Sure you can do this. But you will need to ensure that your base class inherits from System.Web.Page.
So create a library, add the necessary references (System.Web, etc.) and create your base class:
public class MyBasePage: System.Web.Page
{
}
Then you can specify that as your base class for all your other pages.
As for using this single class as the direct code-behind class for all your aspx pages, this may get tricky because the pages will likely have their own individual asp.net controls on them, and therefore the code-behind classes for each page will indeed be slightly different.
I assume you are wanting to do this work because there is a bunch of similar code in the pages, but I imagine there is also some specific code/control declarations as well. So I would create the base class and put the common code in there, and just inherit from that class...
It is possible to move all differences between the sites into ASP.Net Themes or config files? Sounds like a better approach to me
Normally we all do use using System.Linq; and using System.Data.Linq; for example on the code-behind and expect we can reach the members of these namespaces from Source Code like <%= Something.First()%> but when I wrote it, asp.net said it couldn't find First() in the context and I had to add <%# Import Namespace="System.Linq" which looked very weird to me but it worked out. Since they are targeting at the same class why they both need separate namespace importing.
Code-behind :
using System;
using System.Data.Linq;
using System.Linq;
using System.Text
namespace Something
{
class Items : System.Web.UI
{
//...
}
}
but also I need to add the same Linq namespace on the Html Source part
<%#Import Namespace="System.Linq"%>
Do I know something wrong or this is some kind of bug in asp.net. I thought when the page is compiling, asp.net combines these two classes and converts html source code into cs class and indicates the control in Control c= new Control(); hierarchy.
Thanks in advance.
P.s : I am trying to reach for example First() in Items.aspx and everything I mentioned about an asp.net page which is Items.aspx
You must specify your namespaces in both places. It's normal behavior. That's needed by the compiler in order to pre-compile the aspx page and the code-behind page separately, before merging them into one class and doing the actual compilation.
By default, a few common namespaces are already included in the aspx page, so you don't need to import them. But in your case you need to import Linq.
EDIT: And as Joel Coehoorn said, you can add to that list of default namespaces in Web.config, should you not want to manually add them in the aspx pages.
Check your web.config file for a namespaces section and make sure System.Linq is listed there.
Documentation:
http://msdn.microsoft.com/en-us/library/ms164642.aspx
I would not use First in markup, if you still want to do it , make a wrapper in your code behind , like SomeMethod or SomeProperty and access it from markup as <%=SomeProperty %>
First is not a method on the class, but an extension method defined in the System.Linq namespace. Even though you may also use this extension method within the code behind, this doesn't mean that the ASP.NET compiler can find the extension method without a hint - hence the <%# Imports ... %> directive.
Note that the ASP.NET compilation (i.e. of the aspx) is separate from the compilation of the code behind. The latter runs when you build the project; the former runs when you either access the page for the first time, or pre-compile it using "Publish..." or a web deployment project. Hence each compiler needs to be told where to find this extension method if you use it in both places.
I sometimes define Business Logic classes to "help" my ASPX code-behind classes. It makes the most sense to me to include them both in the code-behind file since they work together. However, I'd occasionally like to access the Business Logic classes from higher level classes defined in App_Code but they aren't automatically accessible outside of the file.
Thus, the question: it is easy to access classes defined in App_Code but how do I access classes defined elsewhere?
UPDATE: One other thing, the ASPX page class and the App_Code class are in the same namespace - that isn't the issue.
NOTE: I have taken the advice of those who have responded (thanks guys) and am refactoring to make class access easier. However, I don't think the question is actually answered yet (in the case of an ASP.NET Website project). I don't need the answer any more but, if someone could clarify what makes classes visible when they are outside of App_Code, it may well help someone else (or even me, down the road).
Make sure you place your classes in a sensible namespace.
Place 'using' keyword in code behind files you would like to access them.
Or <%# import if you are using them in inline code.
Put the dll that contains your classes in the /bin folder.
TBH I prefer to keep the separate library project in the same solution and have project reference in the Web probject. VS handles building and placing the dll for you.
Some time while you create or add any file it has been create as the content file while it has to be compile file.
Please follow the process how to resolve this issue.
1) Right click on your class file in App_Code folder.
2) Click on properties.
3) Change Build Action from "" to Compile
4) Rebuild your Application.
This will work definitely
Tushar Tyagi
I assume you mean you are defining a separate class inside the codebehind .cs file? What access modifiers are you giving them?
As above though, I'd generally have a separate project in the solution for this kind of thing (usually in a different namespace like MyApp.Web and MyApp.), and just reference it from there.
You can also just create a standard folder in your project to access classes, and move them there, but you have to do some additional things to make it accessible to your project:
Ensure you don't name the folder any of the reserved terms (i.e. call it "AppCode", instead of "App_Code") if you are running into issues with the "App_Code" folder, after going through the rest of these bullets.
The classes should all have the same namespace as your code-behind.
Ensure the classes are made public, with public methods, if they are being called from other classes.
Include "using MyClass;" statements at the top of the code-behind/class files.
Make sure the class' Build Action property is set to Compile.
You can not access a class from another class in same code behind file as .net dsnt support multiple inheritance. but you can create you business logic class as inner class in main class and make its all functions public so that they can be call in main class.