Something is seriously wrong with my ASP.NET MVC

129 views Asked by At

I have been programming ASP.NET Web Forms for a long time and recently decided to learn ASP.NET MVC.

However, I am clearly having some issues that I cannot tell how. No one that I have come across in web had the same problems.

#1: Web.config issues

When I first created my MVC project in VS2019, there was no web.config so I added it via project > add new item> Web Configuration File. All I needed it for was to store my connection string. When added it had only two lines of code:

<configuration>
</configuration>

I added my connection string and the file looked like this:

<configuration>
 <connectionStrings>
    <add name="connection" connectionString="myconnectionstringblablabla" />
  </connectionStrings>
</configuration>

In my controller I tried to fetch the connection string by doing,

string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

And this happened:

Error

Same things happens when fethcing AppSetting keys from web.config. Is web.config not meant to be used for ASP.NET MVC?

#2 Global.asax

Unlike web.config, Global.asax file doesn't even show up on the add new item menu: add new item menu VS19

Tried this solution: How to add a Global.asax file to ASP.NET MVC5 project Didnt work either.

#3 Session & HttpContext

In ASP.NET Web.Forms, I would normally use Session variables like this:

Session["ClientID"] = somevalue;

I can't do that in ASP.NET MVC, although I have seen people doing it.

Also I can't access to HttpContext from a non-controller class:

HttpContext.Current.Request.Cookies["mycookie"];

It throws HttpContext does not contain a definition for 'Current'...

==================================== Is Something wrong with my program. I don't know how to check if I am using correct references or assemblies, but I think I am missing something.

Please help

0

There are 0 answers