我的asp.net mvc应用程序有问题,在我的解决方案中我有3个项目(我的业务层的dll库项目,网站的asp.net mvc和测试业务层的控制台程序)。当我尝试登录时,我得到了这个例外:
System.ComponentModel.Win32Exception: Paramètre incorrect
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)]
这是我的代码,业务层:
public BaseUser Authenticate(string User, string Password)
{
using(ASTBusinessLayerContext context = new ASTBusinessLayerContext())
{
BaseUser userExists = context.BaseUsers.Where(u => u.Creds.Login == User).First();
if (userExists != null && HashingUtils.VerifyMd5Hash(Password, userExists.Creds.Password))
{
return userExists;
}
else
{
return null;
}
}
}
控制器:
[HttpPost]
public ActionResult Login(FormCollection formCollection)
{
string user = formCollection["User"], pass = formCollection["Password"];
UserBLL bll = new UserBLL();
if(bll.Authenticate(user,pass) != null)
{
return RedirectToAction("Index");
}
else
{
return View();
}
}
App.config(业务层)和Web.config(asp.net mvc):
<connectionStrings>
<add name="SQRASTConnectionString"
connectionString="Server=.\MSSQLSERVER;Database=DBAST; User Id=mydbuser;Password=mypassword;"
providerName="System.Data.SqlClient"/>
注意:当我使用控制台应用程序测试此方法时,它工作正常(我不明白),当我从web.config文件中删除连接字符串时,它不起作用(没有连接字符串)除外)。
所以我的问题是:为什么我必须为所有项目提供连接字符串?
请帮我解决问题。感谢