如何添加以返回视频?



我为每个方法都有一个视图,我让它在标题中设置了返回参数,但是如何制作 VIEW?

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions;
using System;
namespace ProjectAboutProjects
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class FormatReponseFilterAttribute : Attribute, IActionFilter
{
private enum FormatResponseType { Json, Xml, View, Unknown }
private FormatResponseType _requestedType { get; set; }
public void OnActionExecuted(ActionExecutedContext filterContext)
{
var _result = (ObjectResult) filterContext.Result;
switch (_requestedType)
{
//https://localhost:44342/api/Post/Search?id=qwe
case FormatResponseType.Json:
var data = new { Data = _result.Value };
filterContext.Result = new JsonResult(data);
break;
case FormatResponseType.Xml:
filterContext.Result = new XmlResult(_result.Value);
break;
case FormatResponseType.View:
//filterContext.Result = new ViewResult(_result.Value);// Don't worl(
break;
case FormatResponseType.Unknown:
default:
throw new InvalidOperationException("Uknown Content Type ain Accept Header");
}
}
}
}

问题:此行中的return filterContext.Result = new ViewResult (_result.Value);应指向"视图"的哪些内容?

ViewResult 需要更多的数据

filterContext.Result = new ViewResult { 
ViewName = "YourViewName", 
ViewData = // Create your view data here, might be your result
}

还要尝试设置异常处理属性。

filterContext.ExceptionHandled = true;

最新更新