淮北矿业工程建设有限公司网站,大数据是干什么的,有哪些好的做问卷调查的网站好,网站免费搭建Asp.net MVC中Controller返回值类型在mvc中所有的controller类都必须使用Controller后缀来命名并且对Action也有一定的要求#xff1a; 必须是一个public方法必须是实例方法没有标志NonActionAttribute特性的(NoAction)不能被重载必须返回ActionResult类型如: [cs…Asp.net MVC中Controller返回值类型在mvc中所有的controller类都必须使用Controller后缀来命名并且对Action也有一定的要求 必须是一个public方法必须是实例方法没有标志NonActionAttribute特性的(NoAction)不能被重载必须返回ActionResult类型 如: [csharp] view plaincopy public class MyController : Controller { // 必须返回ActionResult类型 public ActionResult HelloWorld() { ViewData[Message] Hello World!; return View(); } } 下面列举Asp.net MVC中Controller中的ActionResult返回类型1、返回ViewResult视图结果将视图呈现给网页 [csharp] view plaincopy public ActionResult About() { return View(); // 参数可以返回model对象 } 2、 返回PartialViewResult部分视图结果主要用于返回部分视图内容在View/Shared目录下创建ViewUserControl.cshtml部分视图 [csharp] view plaincopy public ActionResult UserControl() { ViewBag.Message 部分视图; return PartialView(ViewUserControl); } 页面调用ViewBag.Message 将输出“部分视图”3、 返回ContentResult用户定义的内容类型 [csharp] view plaincopy public ActionResult Content() { return Content(Test Content, text/html); // 可以指定文本类型 } 页面输出“Test Content”此类型多用于在ajax操作中需要返回的文本内容4、 返回JsonResult序列化的Json对象 [csharp] view plaincopy public ActionResult Json() { Dictionarystring, object dic new Dictionarystring, object(); dic.Add(id, 100); dic.Add(name, hello); return Json(dic, JsonRequestBehavior.AllowGet); } 主要用于返回json格式对象可以用ajax操作注意需要设置参数JsonRequestBehavior.AllowGet否则会提示错误此请求已被阻止因为当用在 GET 请求中时会将敏感信息透漏给第三方网站。若要允许 GET 请求请将 JsonRequestBehavior 设置为 AllowGet。5、返回JavaScriptResult可在客户端执行的脚本 [csharp] view plaincopy public ActionResult JavaScript() { string str string.Format(alter({0});, 弹出窗口); return JavaScript(str); } 但这里并不会直接响应弹出窗口需要用页面进行再一次调用。这个可以方便根据不同逻辑执行不同的js操作6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能 [csharp] view plaincopy public ActionResult File() { string fileName ~/Content/test.zip; // 文件名 string downFileName 文件显示名称.zip; // 要在下载框显示的文件名 return File(fileName, application/octet-stream, downFileName); } 直接下载test.zip后保存到本地则为文件显示名称.zip7、 返回Null或者Void数据类型的EmptyResult [csharp] view plaincopy public ActionResult Empty() { return null; } 返回NULL8、重定向方法:Redirect / RedirectToAction / RedirectToRoute Redirect直接转到指定的url地址 [csharp] view plaincopy public ActionResult Redirect() { // 直接返回指定的url地址 return Redirect(http://www.baidu.com); } RedirectToAction直接使用 Action Name 进行跳转也可以加上ControllerName [csharp] view plaincopy public ActionResult RedirectResult() { return RedirectToAction(Index, Home, new { id 100, name liu }); } 也可以带上参数RedirectToRoute指定路由进行跳转 [csharp] view plaincopy public ActionResult RedirectRouteResult() { return RedirectToRoute(Default, new { controller Home, action Index}); } Default为global.asax.cs中定义的路由名称