aspnetcore/src/Microsoft.AspNet.Mvc.Core/Controller.cs

736 lines
31 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Framework.DependencyInjection;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.Mvc
{
public class Controller : IActionFilter, IAsyncActionFilter, IOrderedFilter, IDisposable
{
private DynamicViewData _viewBag;
private IViewEngine _viewEngine;
public IServiceProvider Resolver
{
get
{
return ActionContext?.HttpContext?.RequestServices;
}
}
public HttpContext Context
{
get
{
return ActionContext?.HttpContext;
}
}
public HttpRequest Request
{
get
{
return ActionContext?.HttpContext?.Request;
}
}
public HttpResponse Response
{
get
{
return ActionContext?.HttpContext?.Response;
}
}
public RouteData RouteData
{
get
{
return ActionContext?.RouteData;
}
}
public IViewEngine ViewEngine
{
get
{
if (_viewEngine == null)
{
_viewEngine = ActionContext?.
HttpContext?.
RequestServices.GetRequiredService<ICompositeViewEngine>();
}
return _viewEngine;
}
set
{
_viewEngine = value;
}
}
public ModelStateDictionary ModelState
{
get
{
return ViewData?.ModelState;
}
}
[Activate]
public ActionContext ActionContext { get; set; }
[Activate]
public IUrlHelper Url { get; set; }
[Activate]
public IActionBindingContextProvider BindingContextProvider { get; set; }
public IPrincipal User
{
get
{
return Context?.User;
}
}
[Activate]
public ViewDataDictionary ViewData { get; set; }
public dynamic ViewBag
{
get
{
if (_viewBag == null)
{
_viewBag = new DynamicViewData(() => ViewData);
}
return _viewBag;
}
}
int IOrderedFilter.Order
{
get
{
// Controller-filter methods run closest the action by default.
return int.MaxValue;
}
}
/// <summary>
/// Creates a <see cref="ViewResult"/> object that renders a view to the response.
/// </summary>
/// <returns>The created <see cref="ViewResult"/> object for the response.</returns>
[NonAction]
public virtual ViewResult View()
{
return View(viewName: null);
}
/// <summary>
/// Creates a <see cref="ViewResult"/> object by specifying a <paramref name="viewName"/>.
/// </summary>
/// <param name="viewName">The name of the view that is rendered to the response.</param>
/// <returns>The created <see cref="ViewResult"/> object for the response.</returns>
[NonAction]
public virtual ViewResult View(string viewName)
{
return View(viewName, model: null);
}
/// <summary>
/// Creates a <see cref="ViewResult"/> object by specifying a <paramref name="model"/>
/// to be rendered by the view.
/// </summary>
/// <param name="model">The model that is rendered by the view.</param>
/// <returns>The created <see cref="ViewResult"/> object for the response.</returns>
[NonAction]
public virtual ViewResult View(object model)
{
return View(viewName: null, model: model);
}
/// <summary>
/// Creates a <see cref="ViewResult"/> object by specifying a <paramref name="viewName"/>
/// and the <paramref name="model"/> to be rendered by the view.
/// </summary>
/// <param name="viewName">The name of the view that is rendered to the response.</param>
/// <param name="model">The model that is rendered by the view.</param>
/// <returns>The created <see cref="ViewResult"/> object for the response.</returns>
[NonAction]
public virtual ViewResult View(string viewName, object model)
{
// Do not override ViewData.Model unless passed a non-null value.
if (model != null)
{
ViewData.Model = model;
}
return new ViewResult()
{
ViewName = viewName,
ViewData = ViewData,
ViewEngine = _viewEngine,
};
}
/// <summary>
/// Creates a <see cref="PartialViewResult"/> object that renders a partial view to the response.
/// </summary>
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
[NonAction]
public virtual PartialViewResult PartialView()
{
return PartialView(viewName: null);
}
/// <summary>
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="viewName"/>.
/// </summary>
/// <param name="viewName">The name of the view that is rendered to the response.</param>
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
[NonAction]
public virtual PartialViewResult PartialView(string viewName)
{
return PartialView(viewName, model: null);
}
/// <summary>
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="model"/>
/// to be rendered by the partial view.
/// </summary>
/// <param name="model">The model that is rendered by the partial view.</param>
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
[NonAction]
public virtual PartialViewResult PartialView(object model)
{
return PartialView(viewName: null, model: model);
}
/// <summary>
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="viewName"/>
/// and the <paramref name="model"/> to be rendered by the partial view.
/// </summary>
/// <param name="viewName">The name of the partial view that is rendered to the response.</param>
/// <param name="model">The model that is rendered by the partial view.</param>
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
[NonAction]
public virtual PartialViewResult PartialView(string viewName, object model)
{
// Do not override ViewData.Model unless passed a non-null value.
if (model != null)
{
ViewData.Model = model;
}
return new PartialViewResult()
{
ViewName = viewName,
ViewData = ViewData,
};
}
/// <summary>
/// Creates a <see cref="ContentResult"/> object by specifying a <paramref name="content"/> string.
/// </summary>
/// <param name="content">The content to write to the response.</param>
/// <returns>The created <see cref="ContentResult"/> object for the response.</returns>
[NonAction]
public virtual ContentResult Content(string content)
{
return Content(content, contentType: null);
}
/// <summary>
/// Creates a <see cref="ContentResult"/> object by specifying a <paramref name="content"/> string
/// and a content type.
/// </summary>
/// <param name="content">The content to write to the response.</param>
/// <param name="contentType">The content type (MIME type).</param>
/// <returns>The created <see cref="ContentResult"/> object for the response.</returns>
[NonAction]
public virtual ContentResult Content(string content, string contentType)
{
return Content(content, contentType, contentEncoding: null);
}
/// <summary>
/// Creates a <see cref="ContentResult"/> object by specifying a <paramref name="content"/> string,
/// a <paramref name="contentType"/>, and <paramref name="contentEncoding"/>.
/// </summary>
/// <param name="content">The content to write to the response.</param>
/// <param name="contentType">The content type (MIME type).</param>
/// <param name="contentEncoding">The content encoding.</param>
/// <returns>The created <see cref="ContentResult"/> object for the response.</returns>
[NonAction]
public virtual ContentResult Content(string content, string contentType, Encoding contentEncoding)
{
var result = new ContentResult
{
Content = content,
};
if (contentType != null)
{
result.ContentType = contentType;
}
if (contentEncoding != null)
{
result.ContentEncoding = contentEncoding;
}
return result;
}
/// <summary>
/// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object
/// to JSON.
/// </summary>
/// <param name="data">The object to serialize.</param>
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
/// to JSON format for the response.</returns>
[NonAction]
public virtual JsonResult Json(object data)
{
return new JsonResult(data);
}
/// <summary>
/// Creates a <see cref="RedirectResult"/> object that redirects to the specified <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL to redirect to.</param>
/// <returns>The created <see cref="RedirectResult"/> for the response.</returns>
[NonAction]
public virtual RedirectResult Redirect(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "url");
}
return new RedirectResult(url);
}
/// <summary>
/// Creates a <see cref="RedirectResult"/> object with <see cref="RedirectResult.Permanent"/> set to true
/// using the specified <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL to redirect to.</param>
/// <returns>The created <see cref="RedirectResult"/> for the response.</returns>
[NonAction]
public virtual RedirectResult RedirectPermanent(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "url");
}
return new RedirectResult(url, permanent: true);
}
/// <summary>
/// Redirects to the specified action using the <paramref name="actionName"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(string actionName)
{
return RedirectToAction(actionName, routeValues: null);
}
/// <summary>
/// Redirects to the specified action using the <paramref name="actionName"/>
/// and <paramref name="routeValues"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(string actionName, object routeValues)
{
return RedirectToAction(actionName, controllerName: null, routeValues: routeValues);
}
/// <summary>
/// Redirects to the specified action using the <paramref name="actionName"/>
/// and the <paramref name="controllerName"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName)
{
return RedirectToAction(actionName, controllerName, routeValues: null);
}
/// <summary>
/// Redirects to the specified action using the specified <paramref name="actionName"/>,
/// <paramref name="controllerName"/>, and <paramref name="routeValues"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName,
object routeValues)
{
return new RedirectToActionResult(Url, actionName, controllerName,
TypeHelper.ObjectToDictionary(routeValues));
}
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName)
{
return RedirectToActionPermanent(actionName, routeValues: null);
}
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/> and <paramref name="routeValues"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, object routeValues)
{
return RedirectToActionPermanent(actionName, controllerName: null, routeValues: routeValues);
}
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/> and <paramref name="controllerName"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName)
{
return RedirectToActionPermanent(actionName, controllerName, routeValues: null);
}
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/>, <paramref name="controllerName"/>,
/// and <paramref name="routeValues"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName,
object routeValues)
{
return new RedirectToActionResult(Url, actionName, controllerName,
TypeHelper.ObjectToDictionary(routeValues), permanent: true);
}
/// <summary>
/// Redirects to the specified route using the specified <paramref name="routeName"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoute(string routeName)
{
return RedirectToRoute(routeName, routeValues: null);
}
/// <summary>
/// Redirects to the specified route using the specified <paramref name="routeValues"/>.
/// </summary>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoute(object routeValues)
{
return RedirectToRoute(routeName: null, routeValues: routeValues);
}
/// <summary>
/// Redirects to the specified route using the specified <paramref name="routeName"/>
/// and <paramref name="routeValues"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)
{
return new RedirectToRouteResult(Url, routeName, routeValues);
}
/// <summary>
/// Redirects to the specified route with <see cref="RedirectToRouteResult.Permanent"/> set to true
/// using the specified <paramref name="routeName"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName)
{
return RedirectToRoutePermanent(routeName, routeValues: null);
}
/// <summary>
/// Redirects to the specified route with <see cref="RedirectToRouteResult.Permanent"/> set to true
/// using the specified <paramref name="routeValues"/>.
/// </summary>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(object routeValues)
{
return RedirectToRoutePermanent(routeName: null, routeValues: routeValues);
}
/// <summary>
/// Redirects to the specified route with <see cref="RedirectToRouteResult.Permanent"/> set to true
/// using the specified <paramref name="routeName"/> and <paramref name="routeValues"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues)
{
return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true);
}
/// <summary>
/// Returns a file with the specified <paramref name="fileContents" /> as content and the
/// specified <paramref name="contentType" /> as the Content-Type.
/// </summary>
/// <param name="fileContents">The file contents.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <returns>The created <see cref="FileContentResult"/> for the response.</returns>
[NonAction]
public virtual FileContentResult File(byte[] fileContents, string contentType)
{
return File(fileContents, contentType, fileDownloadName: null);
}
/// <summary>
/// Returns a file with the specified <paramref name="fileContents" /> as content, the
/// specified <paramref name="contentType" /> as the Content-Type and the
/// specified <paramref name="fileDownloadName" /> as the suggested file name.
/// </summary>
/// <param name="fileContents">The file contents.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <param name="fileDownloadName">The suggested file name.</param>
/// <returns>The created <see cref="FileContentResult"/> for the response.</returns>
[NonAction]
public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName)
{
return new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName };
}
/// <summary>
/// Returns a file in the specified <paramref name="fileStream" /> with the
/// specified <paramref name="contentType" /> as the Content-Type.
/// </summary>
/// <param name="fileStream">The <see cref="Stream"/> with the contents of the file.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <returns>The created <see cref="FileStreamResult"/> for the response.</returns>
[NonAction]
public virtual FileStreamResult File(Stream fileStream, string contentType)
{
return File(fileStream, contentType, fileDownloadName: null);
}
/// <summary>
/// Returns a file in the specified <paramref name="fileStream" /> with the
/// specified <paramref name="contentType" /> as the Content-Type and the
/// specified <paramref name="fileDownloadName" /> as the suggested file name.
/// </summary>
/// <param name="fileStream">The <see cref="Stream"/> with the contents of the file.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <param name="fileDownloadName">The suggested file name.</param>
/// <returns>The created <see cref="FileStreamResult"/> for the response.</returns>
[NonAction]
public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)
{
return new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName };
}
/// <summary>
/// Returns the file specified by <paramref name="fileName" /> with the
/// specified <paramref name="contentType" /> as the Content-Type.
/// </summary>
/// <param name="fileName">The <see cref="Stream"/> with the contents of the file.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <returns>The created <see cref="FilePathResult"/> for the response.</returns>
[NonAction]
public virtual FilePathResult File(string fileName, string contentType)
{
return File(fileName, contentType, fileDownloadName: null);
}
/// <summary>
/// Returns the file specified by <paramref name="fileName" /> with the
/// specified <paramref name="contentType" /> as the Content-Type and the
/// specified <paramref name="fileDownloadName" /> as the suggested file name.
/// </summary>
/// <param name="fileName">The <see cref="Stream"/> with the contents of the file.</param>
/// <param name="contentType">The Content-Type of the file.</param>
/// <param name="fileDownloadName">The suggested file name.</param>
/// <returns>The created <see cref="FilePathResult"/> for the response.</returns>
[NonAction]
public virtual FilePathResult File(string fileName, string contentType, string fileDownloadName)
{
return new FilePathResult(fileName, contentType) { FileDownloadName = fileDownloadName };
}
/// <summary>
/// Creates an <see cref="HttpNotFoundResult"/> that produces a Not Found (404) response.
/// </summary>
/// <returns>The created <see cref="HttpNotFoundResult"/> for the response.</returns>
[NonAction]
public virtual HttpNotFoundResult HttpNotFound()
{
return new HttpNotFoundResult();
}
/// <summary>
/// Called before the action method is invoked.
/// </summary>
/// <param name="context">The action executing context.</param>
[NonAction]
public virtual void OnActionExecuting([NotNull] ActionExecutingContext context)
{
}
/// <summary>
/// Called after the action method is invoked.
/// </summary>
/// <param name="context">The action executed context.</param>
[NonAction]
public virtual void OnActionExecuted([NotNull] ActionExecutedContext context)
{
}
/// <summary>
/// Called before the action method is invoked.
/// </summary>
/// <param name="context">The action executing context.</param>
/// <param name="next">The <see cref="ActionExecutionDelegate"/> to execute. Invoke this delegate in the body
/// of <see cref="OnActionExecutionAsync" /> to continue execution of the action.</param>
/// <returns>A <see cref="Task"/> instance.</returns>
[NonAction]
public virtual async Task OnActionExecutionAsync(
[NotNull] ActionExecutingContext context,
[NotNull] ActionExecutionDelegate next)
{
OnActionExecuting(context);
if (context.Result == null)
{
OnActionExecuted(await next());
}
}
/// <summary>
/// Updates the specified model instance using values from the controller's current value provider.
/// </summary>
/// <typeparam name="TModel">The type of the model object.</typeparam>
/// <param name="model">The model instance to update.</param>
/// <returns>true if the update is successful; otherwise, false.</returns>
[NonAction]
public virtual Task<bool> TryUpdateModelAsync<TModel>([NotNull] TModel model)
where TModel : class
{
return TryUpdateModelAsync(model, prefix: typeof(TModel).Name);
}
/// <summary>
/// Updates the specified model instance using values from the controller's current value provider
/// and a prefix.
/// </summary>
/// <typeparam name="TModel">The type of the model object.</typeparam>
/// <param name="model">The model instance to update.</param>
/// <param name="prefix">The prefix to use when looking up values in the value provider.</param>
/// <returns>true if the update is successful; otherwise, false.</returns>
[NonAction]
public virtual async Task<bool> TryUpdateModelAsync<TModel>([NotNull] TModel model,
[NotNull] string prefix)
where TModel : class
{
if (BindingContextProvider == null)
{
var message = Resources.FormatPropertyOfTypeCannotBeNull("BindingContextProvider", GetType().FullName);
throw new InvalidOperationException(message);
}
var bindingContext = await BindingContextProvider.GetActionBindingContextAsync(ActionContext);
return await TryUpdateModelAsync(model, prefix, bindingContext.ValueProvider);
}
/// <summary>
/// Updates the specified model instance using the value provider and a prefix.
/// </summary>
/// <typeparam name="TModel">The type of the model object.</typeparam>
/// <param name="model">The model instance to update.</param>
/// <param name="prefix">The prefix to use when looking up values in the value provider.</param>
/// <param name="valueProvider">The value provider used for looking up values.</param>
/// <returns>true if the update is successful; otherwise, false.</returns>
[NonAction]
public virtual async Task<bool> TryUpdateModelAsync<TModel>([NotNull] TModel model,
[NotNull] string prefix,
[NotNull] IValueProvider valueProvider)
where TModel : class
{
if (BindingContextProvider == null)
{
var message = Resources.FormatPropertyOfTypeCannotBeNull("BindingContextProvider", GetType().FullName);
throw new InvalidOperationException(message);
}
var bindingContext = await BindingContextProvider.GetActionBindingContextAsync(ActionContext);
return await ModelBindingHelper.TryUpdateModelAsync(model,
prefix,
ActionContext.HttpContext,
ModelState,
bindingContext.MetadataProvider,
bindingContext.ModelBinder,
valueProvider,
bindingContext.ValidatorProvider);
}
[NonAction]
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}