1

在 PHP 中,可以立即停止脚本的执行并输出一个变量。例如,如果你想知道 $myVar 中有什么,你可以这样做:

var_dump($myVar); 死();

无论如何要立即获取变量的值,然后在 VB.NET 中将其杀死?

我不是要求必须包含的功能;只是如果有一个本机函数基本上会立即输出一个 var,我不必将它添加到控制器,然后是视图,然后发布和测试。理想情况下,它只会在调试器或消息框或其他东西中输出变量......

我也在使用 ASP.NET MVC 3。

谢谢。

4

1 回答 1

1

您可以使用System.Diagnostics.TraceSystem.Diagnostics.Debug类来打印信息。

停止执行通常通过返回相关的System.Web.Mvc.ActionResult(例如System.Web.Mvc.HttpNotFoundResult )或通过抛出System.Web.HttpException以及最能描述问题的HTTP 状态代码来实现。

这是一个天真的例子。原谅我可怜的VB。

Public Function UpdateUser(id as Integer, userName as String, password as Password) as ActionResult
    Dim user as User = DataServices.GetUserById(id)
    If user Is Nothing Then
        System.Diagnostics.Debug.WriteLine("Aborting because user did not exist.")
        throw new HttpException(404, "Page not found.")
    End
    ' Do more stuff
end

或者在 C# 中

public ActionResult UpdateUser(int id, string userName, string password) {
    var user = DataServices.Users.Where(u => u.id == id).SingleOrDefault();

    if( user == null ) {
        Debug.WriteLine("Aborting because user did not exist.");
        return HttpNotFound(); // Helper method in Controller Class.
    }
    else if( myRoles() < thisUsersRoles(user) ) {
        Debug.Writeline("Aborting because insufficient access.");
        throw new HttpException(401, "Unauthorized");
    }

    // update user here
    return View();
}

请注意,这些示例仅用于显示 Debug 的使用和 http 状态代码的返回。

于 2012-08-01T17:53:43.693 回答