顯示具有 MVC 標籤的文章。 顯示所有文章
顯示具有 MVC 標籤的文章。 顯示所有文章

2024年9月13日 星期五

[筆記]Chrome快取,網頁登入後顯示舊資料

 問題:asp.net MVC 網站登入登出後,畫面維持在舊畫面。
             原本要顯示使用者資訊的區塊還維持在未登入。

  • 到開發人員工具>網路,看頁面上面的要求標頭顯示以下畫面。
  • 停用快取才可以顯示資訊。(才正常)

暫時解法:cache-control改no-store

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Index(){}




其它問題類:參考



2023年6月19日 星期一

[筆記][MVC]JSON長度限制(maxJsonLength)

  • 關鍵字:
    • maxJsonLength

  • 原寫法:
    return Json(result, JsonRequestBehavior.AllowGet);

  • 解法一:
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var result = new ContentResult{
    Content = serializer.Serialize(uList),
    ContentType = "application/json"
    };
    return result;           
  • 解法二:
    var result =GetJson();
    return new JsonResult() {
     Data = result ,
    MaxJsonLength = int.MaxValue,
     JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };



參考之前文章:
  1. [筆記]使用 JSON JavaScriptSerializer 序列化或還原序列化期間發生錯誤,字串的長度超過在 maxJsonLength 屬性上設定的值。

2022年7月11日 星期一

[2022.LEARN.016][筆記]Web API Controller可以Partial嗎?

問題:

  • 同事問:Web API Controller太肥太多方法(主要是不好查)。
  • 不想改路由的情況下,可以像patial class那樣使用嗎?
方法:
  • public partial class TestController:ApiController{程式一}  
  • public partial class TestController:ApiController{程式二}
  • 就是兩個controller都加上partail就可以(當然.cs的檔名要不同或是分開目錄放)。
  • 若有使用[RoutePrefix("Test")],只要其中一個Controller有設定就可以。

2022年4月7日 星期四

[2022.LEARN.009][筆記]MVC站台快取問題

  • 日前踩了個坑 ,(MVC產生的網頁*.html)被快取了。
    後面切換querystring都是抓第一次進入的頁面資訊。

<system.webServer>
<caching>
<profiles>
<add extension=".html" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="23:00:00" />
</profiles>
</caching>
</system.webServer>

2020年3月29日 星期日

[筆記]使用 JSON JavaScriptSerializer 序列化或還原序列化期間發生錯誤,字串的長度超過在 maxJsonLength 屬性上設定的值。


  • 錯誤訊息:
    • 使用 JSON JavaScriptSerializer 序列化或還原序列化期間發生錯誤。字串的長度超過在 maxJsonLength 屬性上設定的值。
  • 參考網址:
  • 解法:以MVC為例
var result =GetJson();
return new JsonResult() {
 Data = result ,
    MaxJsonLength = int.MaxValue,
 JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
//設定maxjsonlength

2019年6月9日 星期日

[筆記]MVC路由傳遞空字串參數

參考網址:
https://stackoverflow.com/questions/17702203/mvc-html-actionlink-removes-empty-querystring-parameter-from-url


@Html.ActionLink("Action",
                 "Controller",
                  new { item1 = new EmptyParameter(), item2 = "value" });

public class EmptyParameter
{
    public override string ToString()
    {
        return String.Empty;
    }
}

2018年7月2日 星期一

[MVC]Task.Run

當MVC Action在叫用 Task<string> functionA() 時,會建議使用
async Task<ActionResut> ActionName() 配合 await funcationA()。

但如果不想改變Action的宣告,作法是使用Task.Run的方式:

ex:

var t =Task.Run(() => functionA());
t.Wait();

參考:

2017年6月6日 星期二

2016年6月28日 星期二

MVC 判斷裝置


@if (Request.Browser.IsMobileDevice) {
  <!-- HTML here for mobile device -->
  <a>mobile</a>
} else {
  <!-- HTML for desktop device -->
  <a>pc</a>
}   

2014年12月10日 星期三

MVC使用錨點

  • 作法一:使用@Html.ActionLink指令
    • @Html.ActionLink("linkText","actionName","controlName","protocol","hostName","fragment",routeValues,htmlAttributes)
    • @Html.ActionLink("測試錨點一", "feature", "Home", null, null, "test1", null, new {@class="123"})
    • 產生的網址為 /Home/feature/#test1
  • 作法二:直接串在原網址後面
    • <a href="@Url.Action("feature", "Home")#test1">測試錨點一</a>
  • 作法三:改路由規則
    以下路由規則為示意
    • routes.MapRoute("fragment", "{action}.html/{id}#{target}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

2013年5月20日 星期一

05_Model建置03

使用傳統ADO.NET的方式建立。以下為取得資料的方式。
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;}    
        }
  }

Controller中的寫法:
public ActionResult Customers(){
    List<models.customer> customers = Models.CustomerDataContext.LoadCustomers();
    return View(customers);
}

參考資料:

2013年5月18日 星期六

04_Model建置02

透過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 "";
}
}
}
}
}


Controller中的寫法:


public ActionResult Customers()
{
List<models.customer> customers = new CustomerDAL().GetAllCustomerData();
return View(customers);
}

2013年5月17日 星期五

03_Model建置01

一般常見的方式使用Entity Framework來建立Model。

image

新增後會進入設定精靈,開始選項有兩種:

1.從資料庫來建立Entity。

2.建立空的Model,之後再讓使用者自行建立相對應的Entity。

在此以1為例。

image

設定連線字串,

選擇No:不含敏感資料

選擇Yes:含敏感資料

image

設定要匯入Model的相關預存、Table、View等,點擊完成後,便會產生對應的資料。

image

點擊EDMX檔案,即可檢視產生的Entity。

設定方法類似TypedDataSet,一樣可以新增欄位等。

image

2013年5月16日 星期四

02_建立MVC專案

開啟VS2012,新增專案,選擇ASP.NET MVC 4 Web應用程式。

image

建立方案後,MVC Web應用程式架構如下:

1.Controllers
2.Models
3.Views

存在相對應目錄來處理每層要做的事情。

SNAGHTML11efe7f

雖然預設MVC Web專案已經把架構目錄都規劃好,但是為了專案彈性,
可以試著把Models獨立成一個專案。

image

再來架構完成後就可以進行Models的設定與建置。

2013年5月15日 星期三

01_MVC概觀

 

層次:

  • Model:模型
  • View:檢視
  • Controller:控制器

clip_image001[1]

ASP.NET:

  • Model:資料模型,資料處理。
    • 常用:
      • Entity Framework
      • ADO.NET
      • Linq to SQL
      • Typed DataSet...etc
  • Controller:控制器,Page和Control
  • View:檢視,UI展示

參考資料: 維基百科