Skip to content

Instantly share code, notes, and snippets.

@hochun836
Last active July 19, 2022 06:02
Show Gist options
  • Select an option

  • Save hochun836/e0c95c0a6ac97cefcd5ca9518f78b3a2 to your computer and use it in GitHub Desktop.

Select an option

Save hochun836/e0c95c0a6ac97cefcd5ca9518f78b3a2 to your computer and use it in GitHub Desktop.
#
Request.Headers["xx"];
Request.QueryString["xx"];
Request.Form["xx"];
Request.Files["xx"];
Request.MapPath();
Response.Headers["xx"] = "yy";
Response.Write("Hello World");
Response.Redirect("https://google.com");
ViewBag.Message = "Your application description page.";
return View();
return Content("Hello World");
Session["xx"] // response 多了個 session cookie
Session.Clear();
Session.Abandon();
Request.Cookies["xx"].Value
Response.Cookies.Add(new HttpCookie("xx") { Value =, Expire = })
Response.Cookies.Add(new HttpCookie("xx") { Value =, Expire = DateTime.Now.AddDays(-1) }) // 清除 Cookie 使用過去的時間
HttpContext.Application["xx"] // 整個項目共有
Server.MapPath()
Server.MachineName
Server.Transfer() // 不能轉發到外站
ViewBag.Conent // Controller // ViewBag dynamic
ViewBag.XX
ViewBag.YY
@ViewBag.Content // View
ViewData["xx"]
@ViewData["xx"]
@ViewBag.xx // ok
TempData["xx"]
@TempData["xx"] // 只能讀取一次
View(string viewName, string masterName, object model)
View(object model)
@model xxx.yyy.Student
@Model.____
@* comment *@
[HttpGet]
public ActionResult Login() {} // 顯示登入頁面
[HttpPost]
public ActionResult Login(string userId, string password) // 顯示登入結果
public ActionResult Login(LoginViewModel vo)
{
if (ModelState.IsValid)
}
[Display(Name ="")]
[EmailAddress]
[EmailAddress(ErrorMessage="")]
[DataType(DataType.Password)]
[Required, MinLength(6)]
[RegularExpression("", ErrorMessage="")]
[Compare(nameof(OtherProeprty))]
[Range(10,20, ErrorMessage="")]
@Html.AntiForgeryToken // 防偽戳
[ValidateAntiForgeryToken]
ModelState.AddModelError(key, errorMessage)
@Html.ValidateSummary()
<label title="游標提示">
@Html.LabelFor(expression, htmlAttribute)
new {@class="", title=""}
ActionResult
ViewResult // 返回視圖, View()
ContentResult // 返回字串, Content()
RedirectResult // 重定向, Redirect()
RedirectToReouteResult // 重定向, RedirectToReouteResult(actionName, controllerName)
FileContentResult
FilePathResult
FileStreamResult
JsonResult // 返回 JSON, Json()
HttpStatusCodeResult // new HttpStatusCodeResult(HttpStatusCode.XXX)
PartialViewResult // 返回部分頁面 (相當如組件 Component), PartialView()
public ActionRsult Upload(HttpPostedFileBase file)
@Html.Action("PartialView") // 呼叫 Action 來處理
@Html.Partial("_Login") // 沒有 Action 來處理
@Html.Partial("_Login", ViewDataDictionary viewData)
@Html.Partial("_Login", object model)
@model List<Student>
@{
c# statement;
c# statement;
c# statement;
}
@variable
@(c# expression)
@@ // @
@gmail.com // 自動識別
@Html.Raw()
@foreach (var x in Model)
{
<li>@x.Name</li>
<span>a+100</span>
a+100
@(a+100)
}
區域 (area) 可以認為是一個獨立的 MVC (會多一個 Areas 資料夾)
=> 造成外層 RouteConfig 找到多個 Controller
=> 所以外層 RouteConfig 要補上外層 Controller 的命名空間
<a href="/Sport/xxx/yy"> // 不優
@Html.RouteLink(string linkText, string routerName, object routeValues) // 跨區域路由跳轉
new { controller="", action="" }
@Html.ActionLink(string linkText, string actionName, object routeValues) // 當前區域路由跳轉
new { xxx="" }
<a href="@Url.RouteUrl(string routerName, outeValueDictionary routeValues)"> // 產生路徑
<a href="@Url.Action(string actionName, RouteValueDictionary routeValues)"> // 產生路徑
Action 不給他人訪問
way 1. public => private
way 2. 加上 [NoAction]
Action 換個名稱
加上 [ActionName("xxx")]
# [note] learn
=> ref: https://www.bilibili.com/video/BV1mt41177NZ
=> ref: https://www.bilibili.com/video/BV1bX4y1P7zt
---
AspNetCoreMvc
ViewData.Model == Model
@Html.IdFor(model => Model.Count)
@Html.IdFor(model => model.Count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment