我的ASP.NET MVC严重错误
我已经编程了ASP.NET Web表单已经很长时间了,最近决定学习ASP.NET MVC。
但是,我显然遇到了一些我无法分辨的问题。我在网络中遇到的人没有同样的问题。
#1:web.config问题
当我第一次在VS2019中创建我的MVC项目时,没有web.config,因此我通过 project>添加新项目> Web配置文件。 我所需要的只是存储我的连接字符串。当添加时,它只有两行代码:
<configuration>
</configuration>
我添加了连接字符串,并且文件看起来像这样:
<configuration>
<connectionStrings>
<add name="connection" connectionString="myconnectionstringblablabla" />
</connectionStrings>
</configuration>
在控制器中,我尝试通过执行来获取连接字符串,
string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
这发生了:
> Web.config的键。 Web.config不适用于ASP.NET MVC吗?
#2 global.asax
与web.config不同,global.asax文件甚至都不显示在添加新项目菜单:
尝试了此解决方案:如何将global.asax文件添加到ASP.NET MVC5项目也没有工作。
#3会议&amp; httpcontext
在asp.net web.forms中,我通常会使用这样的会话变量:
Session["ClientID"] = somevalue;
尽管我已经看到人们这样做,但我无法在ASP.NET MVC中这样做。
另外,我无法从非控制器类访问httpcontext:
HttpContext.Current.Request.Cookies["mycookie"];
它抛出 httpcontext不包含“当前” ...
====================================== ===================== 我的程序有问题。我不知道如何检查我是否正在使用正确的引用或组件,但我认为我缺少一些东西。
请帮忙
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:
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论