私は、城ウィンザーファクトリを使用してリクエストURLに基づいてオブジェクトをインスタンス化しています。どのように応答を終了し、HTTPコード404を送り返すことができますか?
ような何か:いくつかのケースでは
public FooViewModel Get()
{
if (HttpContext.Current == null)
{
return new FooViewModel();
}
var currentContext = new HttpContextWrapper(HttpContext.Current);
// resolve actual view model.
、私は実際に404を投げると、要求を停止する、現在のように:
throw new HttpException(404, "HTTP/1.1 404 Not Found");
currentContext.Response.End();
要求がまだ終了していませんがアクションに当たってビューを解決しようとしていますか?私はこのすべて間違っについて
public class HomeController : Controller
{
public FooViewModel Foo { get; set; }
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
を考えています:
私のコントローラは、次のようになりますか?それとも私がこれを達成する方法はありますか?
私が考えていた代替案は、Fooプロパティの状態を確認するアクションの属性ですか?
public class RequiresModelAttribute : ActionFilterAttribute
{
private readonly string _modelName;
public RequiresModelAttribute(string modelName)
{
_modelName = modelName;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var property = filterContext.Controller.GetType().GetProperty(_modelName);
var model = property.GetValue(filterContext.Controller);
if (model == null)
{
filterContext.Result = new HttpStatusCodeResult(404);
}
}
}
を次に、あなたが行うことができ、あなたのコントローラ上:
私はこれを既にこれに変更しました。他の提案があるかどうか確認してください。 – shenku
@shenku私は別のアプローチで自分のレスポンスを更新しました。おそらく、あなたのシナリオでうまくいくでしょうか? –