asp.net-mvc – 在ASP.NET MVC中实现自定义配置文件提供程序
|
我尝试了很多在ASP.NET MVC中实现自定义配置文件提供程序.
我已经阅读了很多很多教程,但我无法找到问题所在.它与 Implementing Profile Provider in ASP.NET MVC非常相似. 但是我想创建自己的Profile Provider,所以我编写了以下继承自ProfileProvider的类: public class UserProfileProvider : ProfileProvider
{
#region Variables
public override string ApplicationName { get; set; }
public string ConnectionString { get; set; }
public string UpdateProcedure { get; set; }
public string GetProcedure { get; set; }
#endregion
#region Methods
public UserProfileProvider()
{ }
internal static string GetConnectionString(string specifiedConnectionString)
{
if (String.IsNullOrEmpty(specifiedConnectionString))
return null;
// Check <connectionStrings> config section for this connection string
ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings[specifiedConnectionString];
if (connObj != null)
return connObj.ConnectionString;
return null;
}
#endregion
#region ProfileProvider Methods Implementation
public override void Initialize(string name,System.Collections.Specialized.NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("config");
if (String.IsNullOrEmpty(name))
name = "UserProfileProvider";
if (String.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description","My user custom profile provider");
}
base.Initialize(name,config);
if (String.IsNullOrEmpty(config["connectionStringName"]))
throw new ProviderException("connectionStringName not specified");
ConnectionString = GetConnectionString(config["connectionStringName"]);
if (String.IsNullOrEmpty(ConnectionString))
throw new ProviderException("connectionStringName not specified");
if ((config["applicationName"] == null) || String.IsNullOrEmpty(config["applicationName"]))
ApplicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
else
ApplicationName = config["applicationName"];
if (ApplicationName.Length > 256)
throw new ProviderException("Application name too long");
UpdateProcedure = config["updateUserProcedure"];
if (String.IsNullOrEmpty(UpdateProcedure))
throw new ProviderException("updateUserProcedure not specified");
GetProcedure = config["getUserProcedure"];
if (String.IsNullOrEmpty(GetProcedure))
throw new ProviderException("getUserProcedure not specified");
}
public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context,System.Configuration.SettingsPropertyCollection collection)
{
SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(GetProcedure,myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@FirstName",(string)context["FirstName"]);
try
{
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.SingleRow);
reader.Read();
foreach (SettingsProperty property in collection)
{
SettingsPropertyValue value = new SettingsPropertyValue(property);
if (reader.HasRows)
{
value.PropertyValue = reader[property.Name];
values.Add(value);
}
}
}
finally
{
myConnection.Close();
myCommand.Dispose();
}
return values;
}
public override void SetPropertyValues(System.Configuration.SettingsContext context,System.Configuration.SettingsPropertyValueCollection collection)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(UpdateProcedure,myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
foreach (SettingsPropertyValue value in collection)
{
myCommand.Parameters.AddWithValue(value.Name,value.PropertyValue);
}
myCommand.Parameters.AddWithValue("@FirstName",(string)context["FirstName"]);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
myCommand.Dispose();
}
}
这是我的Controller中的CreateProfile操作: [AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateProfile(string Username,string Password,string FirstName,string LastName)
{
MembershipCreateStatus IsCreated = MembershipCreateStatus.ProviderError;
MembershipUser user = null;
user = Membership.CreateUser(Username,Password,"test@test.com","Q","A",true,out IsCreated);
if (IsCreated == MembershipCreateStatus.Success && user != null)
{
ProfileCommon profile = (ProfileCommon)ProfileBase.Create(user.UserName);
profile.FirstName = FirstName;
profile.LastName = LastName;
profile.Save();
}
return RedirectToAction("Index","Home");
}
我的程序usp_GetUserProcedure没什么特别的: ALTER PROCEDURE [dbo].[usp_GetUserProcedure] -- Add the parameters for the stored procedure here @FirstName varchar(50) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT * FROM dbo.Users WHERE FirstName = @FirstName END 我的Web.Config文件: <profile enabled="true"
automaticSaveEnabled="false"
defaultProvider="UserProfileProvider"
inherits="Test.Models.ProfileCommon">
<providers>
<clear/>
<add name="UserProfileProvider"
type="Test.Controllers.UserProfileProvider"
connectionStringName="ApplicationServices"
applicationName="UserProfileProvider"
getUserProcedure="usp_GetUserProcedure"
updateUserProcedure="usp_UpdateUserProcedure"/>
</providers>
</profile>
但我总是得到这个例外:
关于我可能做错什么的任何想法? 解决方法
最可能的原因是
myCommand.Parameters.AddWithValue("@FirstName",(string)context["FirstName"]);
(string)context [“FirstName”]是一个空值.即使将参数传递给sproc,如果所需参数的值为null,那么您将看到此错误. SQL Server(有效地)不区分未传递的参数和使用空值传递的参数. 你看到一个SQL错误.这与MVC无关,MVC并没有真正导致您的问题.确定null是否为有效值上下文[“FirstName”],如果是,则将函数更改为接受空值.如果没有,找出为什么context [“FirstName”]为空. 此外,我认为这一行不会正确添加您的参数名称(使用“@”前缀). myCommand.Parameters.AddWithValue(value.Name,value.PropertyValue); 此外,由于这是MVC,请确保在表单上有一个名为FirstName的控件: public ActionResult CreateProfile(string Username,string LastName) 它根据名称而不是ID读取字段 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 带有内联样式表和媒体查询的Razor视图
- ASP.NET如何访问公共属性?
- asp.net-mvc-4 – Web API 2返回OK响应,但在后台继续处理
- asp.net – 浏览器的缓存是否考虑了URL中的参数? (ASPX页面
- asp.net – 直接将.aspx转换为.pdf [已关闭]
- asp.net – 运行时从Microsoft.AspNet.WebApi.HelpPage版本
- asp.net – 如何忽略Model属性的验证?
- ASP.NET MVC jquery自动填充值和文本字段
- asp.net – 如何在json结果上使用jquery选择器
- asp.net-mvc-4 – / signalr / hubs未加载到asp.net mvc4:
