在控制器 HomeController.cs 中使用以下代码
public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } public ActionResult Display() { var name = (string)RouteData.Values["id"]; var model = string.Empty; // model = "Test"; // 如果为空 则返回自身 // 如果为不存在的视图 则报错 if (model == null) { // Url 也会跟随变化为 /home/NotFound return RedirectToAction("NotFound"); } return View(model); } public ActionResult NotFound() { return View(); } public FileResult DownloadPicture() { var name = (string)RouteData.Values["id"]; var picture = "/Content/Picture/" + name + ".jpg"; var contentType = "image/jpg"; /* // 下载后自动返回到上一页 var fileName = name + ".jpg"; return File(picture, contentType, fileName); * */ // 在浏览器显示图片 return File(picture, contentType); } public HttpStatusCodeResult UnauthorizedError() { // 这将跳转到登录页面 web.config 里指定路径 return new HttpUnauthorizedResult("Custom Unauthorized Error"); } public ActionResult NotFoundError() { return HttpNotFound("Nothing here..."); }
测试各种 Result 使用方法.