在web.config文件中的应用程序设置中设置默认值

时间:2015-08-24 05:55:42

标签: asp.net-mvc-4

在我的项目中,我有用户类型表,其中我只存储了2个ID

UserType

Usertype id Name
3 Customer User
5 Business User

我需要在我的MVC项目中的web.config文件中设置2个ID。在进入代码时,我们需要从web.config文件中调用它。是否可以

我在下面的代码

中硬编码了这个值
public ActionResult Calendar(int id)
{

var cal = (from s
in db.tblUsers
where s.UserTypeId == 5 && s.UserID == id
select new Appt
{
FirstName = s.FirstName,
BusinessCategory = s.tblBusinessCategory.BusinessCategory,
BusinessName = s.tblBusinessCategory.BusinessName,
StartTime = s.WorkingHour.StartTime,
UserTypeID =5,
UserID = s.UserID
}).FirstOrDefault();
Session["BUId"] = id;
return View(cal);

}

在usertype id中我们需要从web.config获取值。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

您可以在web.config中使用<appSettings>部分,如下所示,并使用ConfigurationManager.AppSettings["usertypeid"]

读取该值
<appSettings>
    <add key="usertypeid" value="5"/>
</appSettings>