問題:asp.net MVC 網站登入登出後,畫面維持在舊畫面。
原本要顯示使用者資訊的區塊還維持在未登入。
暫時解法:cache-control改no-store
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Index(){}
其它問題類:參考
問題:
<system.webServer>
<caching>
<profiles>
<add extension=".html" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="23:00:00" />
</profiles>
</caching>
</system.webServer>
var result =GetJson();
return new JsonResult() {
Data = result ,
MaxJsonLength = int.MaxValue,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
//設定maxjsonlength
@Html.ActionLink("Action",
"Controller",
new { item1 = new EmptyParameter(), item2 = "value" });
public class EmptyParameter
{
public override string ToString()
{
return String.Empty;
}
}
當MVC Action在叫用 Task<string> functionA() 時,會建議使用
async Task<ActionResut> ActionName() 配合 await funcationA()。
但如果不想改變Action的宣告,作法是使用Task.Run的方式:
ex:
var t =Task.Run(() => functionA());
t.Wait();
參考:
參考:
[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]public class HomeController : Controller
@if (Request.Browser.IsMobileDevice) {
<!-- HTML here for mobile device -->
<a>mobile</a>
} else {
<!-- HTML for desktop device -->
<a>pc</a>
}
namespace MvcApplication1.Models
{
public class CustomerDataContext {
List<customer> GetAllCustomerData(){
List<customer> customers = new List<customer>();
SqlConnection conn = new SqlConnection("initial catalog=northwind; integrated security=SSPI");
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
Customer customer = new Customer();
for (int i = 0; i < reader.FieldCount; i++)
{
PropertyInfo property =
customer.GetType().GetProperty(reader.GetName(i));
property.SetValue(customer,
(reader.IsDBNull(i)) ? "[NULL]" : reader.GetValue(i), null);
}
customers.Add(customer);
}
reader.Close();
return customers;}
}
}
public ActionResult Customers(){
List<models.customer> customers = Models.CustomerDataContext.LoadCustomers();
return View(customers);
}
透過Entity Framework取得資料庫資料。
namespace Project.Models
{
public class CustomerDAL
{
///
/// 取得本機驗證碼
///
///
public List<ustomer> GetAllCustomerData()
{
using (myDBEntities obj = new myDBEntities())
{
try
{
//使用linq語法來取得(篩選)資料
var query = (from c in obj.Customer
select c);
return query.ToList<Customer>();
}
catch (Exception ex)
{
return "";
}
}
}
}
}
public ActionResult Customers()
{
List<models.customer> customers = new CustomerDAL().GetAllCustomerData();
return View(customers);
}
一般常見的方式使用Entity Framework來建立Model。
新增後會進入設定精靈,開始選項有兩種:
1.從資料庫來建立Entity。
2.建立空的Model,之後再讓使用者自行建立相對應的Entity。
在此以1為例。
設定連線字串,
選擇No:不含敏感資料
選擇Yes:含敏感資料
設定要匯入Model的相關預存、Table、View等,點擊完成後,便會產生對應的資料。
點擊EDMX檔案,即可檢視產生的Entity。
設定方法類似TypedDataSet,一樣可以新增欄位等。
開啟VS2012,新增專案,選擇ASP.NET MVC 4 Web應用程式。
建立方案後,MVC Web應用程式架構如下:
1.Controllers
2.Models
3.Views
存在相對應目錄來處理每層要做的事情。
雖然預設MVC Web專案已經把架構目錄都規劃好,但是為了專案彈性,
可以試著把Models獨立成一個專案。
再來架構完成後就可以進行Models的設定與建置。