10

キャッスル ウィンザー ファクトリを使用して、リクエスト URL に基づいてオブジェクトをインスタンス化しています。

何かのようなもの:

    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 プロパティの状態をチェックするアクションの属性ですか?

4

2 に答える 2

12

アクションフィルターを使用するアプローチは、あなたが望むものを達成できると思います:

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);
        }
    }
}

次に、コントローラーで次のことができます。

public class HomeController : Controller
{
    public FooViewModel Foo { get; set; }

    [RequiresModel("Foo")]
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }
}

編集:おそらく、スローされた HttpExceptions を尊重するためにグローバルフィルターを使用していますか?

public class HonorHttpExceptionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var httpException = filterContext.HttpContext.AllErrors.FirstOrDefault(x => x is HttpException) as HttpException;
        if (httpException != null)
        {
            filterContext.Result = new HttpStatusCodeResult(httpException.GetHttpCode());
        }
    }
}

次に Global.asax で:

  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
      filters.Add(new HandleErrorAttribute());
      filters.Add(new HonorHttpExceptionAttribute());
  }
于 2012-09-12T00:31:09.867 に答える
2

別のオプションは、コントローラーで OnException をオーバーライドすることです。

 public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        Get();
        return View();
    }

    public int Get()
    {
        throw new HttpException(404, "HTTP/1.1 404 Not Found");
        // we don't need end the response here, we need go to result step
        // currentContext.Response.End();

    }

    protected override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);
        if (filterContext.Exception is HttpException)
        {
            filterContext.Result = this.HttpNotFound(filterContext.Exception.Message);
        }
    }
于 2012-09-12T01:45:59.760 に答える