zoukankan      html  css  js  c++  java
  • [MVC 4] ActionResult 使用示例

    在控制器 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 使用方法.

  • 相关阅读:
    django之分页
    linux后台运行和关闭、查看后台任务
    Django的模板系统
    Django的视图系统
    Django的配置文件(settings.py)
    Django的URL路由
    初始Django
    shell if判断总结
    一个抓取智联招聘数据并存入表格的python爬虫
    Python MySQLdb 查询中文出现问号的解决方法
  • 原文地址:https://www.cnblogs.com/z5337/p/5395651.html
Copyright © 2011-2022 走看看