English 中文(简体)
在ASP中设立吉大港山区地位。 NET MVC控制器结果并不使人认为
原标题:Setting HTTP Status in ASP.NET MVC controller results does not render view

我有一个习惯行动,恢复某些吉大港定居地,如诺福迪德和福布特劳伦,他们都来自观望。

如果一个实体在行动过程中没有在数据库中找到,我就利用它们进行短路行动。

在这些结果中,我将吉大港山区的状况确定为适当数目。 当我这样做时,我认为这些观点并没有提出。 我不得不离开科索沃200个,以便我能够发表意见。

我如何在伙伴关系中确定适当的地位和看法。 NET MVC 2.0?

最佳回答

I have a custom ActionResult for returning certain HTTP Errors, like NotFoundResult and ForbiddenResult, they all derive from ViewResult.

请允许我建议另一种错误处理方式:

首先是创建错误控制器和相应的观点:

public class ErrorController : Controller
{
    public ActionResult General()
    {
        return View();
    }

    public ActionResult HttpError404()
    {
        return View();
    }

    public ActionResult HttpError500()
    {
        return View();
    }
}

http://www.ohchr.org。 定义<代码> 申请__ Error 方法:

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    // TODO: Log the exception with your favorite logging framework

    Response.Clear();
    var httpException = exception as HttpException;

    var routeData = new RouteData();
    // Take the ErrorController
    routeData.Values.Add("controller", "error");

    if (httpException == null)
    {
        // Use the General action for any unhandled error
        routeData.Values.Add("action", "general");
    }
    else
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                routeData.Values.Add("action", "httpError404");
                break;
            case 500:
                routeData.Values.Add("action", "httpError500");
                break;
            default:
                routeData.Values.Add("action", "general");
                break;
        }
    }

    // Add the exception to route data so that the error controller 
    // could use it with RouteData.Values["error"]
    routeData.Values.Add("error", exception);

    Server.ClearError();
    IController errorController = new ErrorController();
    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}

最后提出适当的例外:

public class HomeController: Controller
{
    public ActionResult Index(int id)
    {
        var model = _repository.GetModel(id);
        if (model == null)
        {
            throw new HttpException(404, "Model not found with id = " + id);
        }
        return View(model);
    }
}
问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签