Inaccessible public classes - c#

I'm using a public LoginContext class to manage user logins in my web app.
Unfortunately, even though I have the LoginContext class declared publicly, my partial class Login at Login.aspx.cs can't seem to access it.
My code is as follows:
// ~/App_Code/LoginContext.cs
namespace stman
{
public class LoginContext
{
}
}
// ~/Login.aspx.cs
namespace stman
{
public partial class Login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
LoginContext log = new LoginContext(); // error is here
}
}
}
The error that comes up on the line where I instantiate LoginContext reads as follows:
The type or namespace name 'LoginContext' could not be found (are you missing a using directive or an assembly reference?)
When I try to generate a new class for LoginContext, it goes into the web app's root folder where it can no longer access the public Database class that I need in LoginContext.
I have no idea what's causing all of these problems, but based on what I've learned over the last 18 months doing this professionally, they shouldn't exist right now...
Can anyone help clear things up here? Specifically I'd like to know:
What I'm doing wrong
Why it's wrong
Who can I fix it?
Thanks in advance!
EDIT
I've had a look and it seems neither the Database class in ~/App_Code/Database.cs or the LoginContext class in ~/App_Code/LoginContext.cs are accessible to the page - or any page in the website.

In LoginContext.cs properties, marked it as BuildAction = Compile.

You can achieve this behaviour when these classes are located in different projects.
If this is true then you should use full path to the class starting from the project name
ProjectName.stman.LoginContext

try using constructor
namespace stman
{
// Database is a class that handles the sql queries and such
public class LoginContext : Database
{
public LoginContext () : base("Name=LoginContext")
{
}
}
}

From what I read from the App_Code behavior may be different in web site projects and web application. I wonder what kind of project you're working on? One possible solution would be to make this project in web application project, this Link can help you to make this project:
Source

Related

c#: Problems with the namespaces

I am having a problem with Visual Studio 2015 and Framework 4.6.1.
I have a business layer with this namespace: BusinessLayer.LocalStorage. In this class (named LocalStorage) I have this function:
...
public static void XXX()
{
}
...
In the main project with the same Framework 4.6.1, I have in one Winform
using BusinessLayer.LocalStorage;
In the Load I wrote the function but the reference is not recognized:
I have to add LocalStorage before:
LocalStorage.LocalStorage.XXX();
This way the call is working
why is not working using LocalStorage.XXX()? I declared my namespace in the using clausules.. Then should be working.
I have the class in a directory inside BusinessLayer project for this reason namespace should be BusinessLayer.LocalStorage
Code is:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
...
public static void XXX()
{
}
...
}
}
This is because LocalStorage is ambiguous, it could either be namespace LocalStorage or the class LocalStorage. You can solve this in 3 ways:
Remove the namespace LocalStorage entirely and move everything in it inside the BusinessLayer namespace
Rename the class LocalStorage to something else
Add using LocalStorage = BusinessLayer.LocalStorage.LocalStorage to the top of your file where you need to use the class LocalStorage
Edit: Option 4. If you desperately want to keep the directory structure as it is right now (with a LocalStorage directory), you can alternatively tell Visual Studio that the directory LocalStorage is not a namespace provider. You can do this by editing the properties of the folder in the Solution Explorer
Visual studio automatically generates Namespaces based on the current location of a class within a solution. When you create a new class it first takes the name of the project. Then it takes the current folder the class is located in. it will continue appending the folder name to the namespace until it reaches the class name itself.
So for example, if you have a project called ExampleProject, with a folder called Models, with a class inside called BaseModel.cs, the resulting namespace would be ExampleProject.Models
Make sure the namespace containing XXX is not LocalStorage.LocalStorage and is called BusinessLayer.LocalStorage. You can change this namespace to whatever you want and rebuild your project.
Although the namespace structure and object names are not recommended to be the same, which will lead to errors in many cases, I see that you want to use it this way from the comments you made.
I don't recommend you to name a class like its namespace, see this article
I would recommend would be <XYZ>LocalStorage.cs
I don't know your layer structure, but you can call it LocalStorage.XXX () with a structure like the one below.
BusinessLayer structure:
LocalStorage Class:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
public static void XXX()
{
}
}
}
And finally, after referencing BusinessLayer to winForm, you can call it in Load method as follows.
using System;
using BusinessLayer.LocalStorage;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LocalStorage.XXX();
}
}
}
Or, if you don't want a folder in your BusinessLayer structure, you can use it using the same namespace (namespace BusinessLayer.LocalStorage) as below.

Argument type is not assignable to parameter type although it inherits

I tried to write a MVC application with ASP.net Identity from scratch.
To do so I followed two tutorials from Ben Foster (Tutorial Part1 and Tutorial Part2)
But I stuck at the second tutorial - Configuring UserManager. The following line doesn't work for me:
// configure the user manager
UserManagerFactory = () =>
{
var usermanager = new UserManager<AppUser>(
new UserStore<AppUser>(new AppDbContext()));
...
}
Visual Studio underlines
new AppDbContext()
and shows me the following message:
Argument type "MyProject.DbContext.AppDbContext" is not assignable to parameter type "System.Data.Entity.DbContext"
I don't understand why it doesn't work in my solution because I followed completely the tutorial. My AppDbContext looks like:
namespace MyProject.DbContext
{
using MyProject.Models;
using Microsoft.AspNet.Identity.EntityFramework;
public class AppDbContext : IdentityDbContext<User>
{
public AppDbContext()
: base("DefaultConnection")
{
}
}
}
My User class:
namespace MyProject.Models
{
using Microsoft.AspNet.Identity.EntityFramework;
public class User : IdentityUser
{
public string Name{ get; set; }
}
}
I also downloaded the source code from Ben and tried to run it and it works without any problems. I think it doesn't make any difference that all of my files aren't located in the same folder?!
I hope you can help me. It's really frustrating if a simple tutorial doesn't work as it should...
Regards, winklerrr
I solved the problem by removing all the references that had something to do with owin and ASP.net Identity and re-added them again.
I think, the problem was caused by a discrepancy between the referenced dlls and the actual used dlls... (Played to much with the package manager console.)
Your UserManager takes AppUser as generic type: UserManager<AppUser>, when you db-context takes User as generic param. AppUser and User are not the same classes.
Your user-defining class should be the same everywhere.

Unable to access class from app_code

I have a web site(not web app) which has a default.aspx and default.aspx.cs.
And it has an App_Code folder with class A in it.
So default.aspx.cs. Looks like:
namespace test1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
And class A:
namespace test1
{
public class A
{
_Default mDefaultRef;
public pageLogicIndividual(_Default defaultRef)
{
mDefaultRef = defaultRef;
}
}
}
I can use A in _Default. eg. if I type test1., while working in class _Default, the IntelliSense will show both classes.
But if I type test1., while working in class A, the IntelliSense will only show A.
Why can't I use _Default in A?
Error : the type or namespace name does not exist in the namespace
(are you missing an assembly reference )
Edit:
I'll try to clarify.
While working in the .cs file where class _Default resides I can type test1., which is the namespace, and the intellisense will show me class _Default and Class A.
But if I'm working in the .cs file where class A resides and type test1., which is the namespace, then the intellisense will only show me class A.
I have had this challenge in the past.
Open up the App_Code folder node in Visual studio.
Right click on the concerned class, then click properties
In the properties pane, change Build Action to Compile.
It should work fine now.
Your problem is with your misleading namespace that you've added yourself after the new file has been created because ASP.NET Web Sites do not have namespace. The namespaces are available in Web Applications projects. i.e. after a new WebSite is created, namespace doesn't added to the files.
So you don't need to place your class A inside the test1 namespace because you can use A in default.aspx.cs even without namespace but you can not access other WebForm page classes from a Webform page or App_Code classes.
BTW if you want to use the necessary and reusable methods within a class of the Default Web Form, you can move those methods out to A class which is under App_Code and as I said already you can use it within all the Web Form CodeFiles without providing namespace for it.
In a nutshell, you cannot access page classes from App_code classes.
This restriction comes from website project compilation model. Special App_code folder contains shared classes (and possibly other resources) which are available to pages (and to each other). During compilation App_code is compiled first in a single assembly. This is the key point. Pages, controls and MasterPages are compiled in another assembly(ies) which may have references to the App_code assembly (but not vise versa). So this is one-way road.
No namespace declaration should circumvent this behavior.
Pages can see each other in ASP namespace (ASP.default_aspx) but pages usually don't have public properties / methods (user controls .ascx usually have).
Read better explanation on MSDN Code-Behind Pages

a small questions on web part, sharepoint

I created a empty web part, then created a usercontrol, in UserControl.ascx.cs, I wrote like that:
namespace tasks_email.ControlTemplates.tasks_email
{
public partial class UserControl1 : UserControl
{
public tasks_email WebPart { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
then it said:"tasks_email.Controltemplates.tasks_email" is a 'namesspace' but is used like a 'type'
Can any body tell me where the problem is and give me some suggestion?
If you want to create a webpart with a user control inside of it you can just use the "Visual web part" template in visual studio. It will create a webpart, and a user control, and add the user control to the webpart all for you, and even package the whole thing into one collapsible component in solution explorer. It's quite convenient. Doing all of that work yourself (for every new webpart) is just a pain.
namespace tasks_email.ControlTemplates.tasks_email
Here you declare tasks_email as a namespace.
public tasks_email WebPart { get; set; }
Here you declare a member called WebPart, with a type tasks_email. That's not a type - you've already said it's a namespace. I can't guess what you really meant.
Did you try "namespace tasks_email.ControlTemplates"? Because the way you do it, you use tasks_email like a type indeed ; and not as a namespace

Accessing Page Class From Another Page Class

How do I access a page class from another class. For example I have:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
Why can I not access it from another class in App_Code folder?
public class MyClass
{
public MyClass() {}
public void DoSomething(object o)
{
// The following line won't compile.
MyPage page = o as MyPage;
}
}
I just figured it out (thanks to Fujiy) that for some reason this is the case with website project, but is not a problem with web application project in VS. If anyone has any clues as to why, please share your thoughts. Thank you :)
I see nothing wrong with your code as posted, most likely you've got a namespace problem.
edit: gah, just noticed that you mentioned this was a website project. It's been a while since I deigned to start one of those :) but I believe this stems from the fact that App_Code is run-time compiled. It would take a better man than me to explain why that creates the problem, but long story short I'd just avoid website projects in general.
You can do this, afaik:
System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
Be careful where you call that though, because obviously at different points in your code the page won't be available. For example, Application_Start in global.asax.

Categories