[英]MVC + returning different PartialView on condition
Not sure if this is the best approach in MVC but how do I return views on condition, let's say if I want to return another view which displays some error message if my 'fbUID' is missing, please kindly assist. 不确定这是否是MVC中最好的方法,但是如果有条件的话我该如何返回视图,如果我想返回另一个视图,如果我的'fbUID'丢失了,该视图会显示一些错误消息,请提供帮助。 Thanks.
谢谢。
public PartialViewResult GetCredentials(string facebookUID, string facebookAccessTok)
{
string fbUID = facebookUID;
if (fbUID != null)
{
// Request fb profile pic
var rawImg = new Bitmap(ImageHelper.requestBitmapImage(fbUID));
var processblurredImg = new Bitmap(rawImg);
var gb = new GaussianBlur();
for (int i = 0; i < 8; i++)
{
gb.ApplyInPlace(processblurredImg);
}
// Download it to local drive / server
string uploadPath = Server.MapPath("~/upload");
string fullPath = uploadPath + "\\ProfilePic.png";
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
if (uploadPath != null)
{
ImageHelper.savePng(fullPath, processblurredImg, 500L);
}
return PartialView("BlurredPhoto");
}
return PartialView("TestPartialView"); //if fbUID is null
}
Have a look at action filters. 看一下动作过滤器。 These allow you to install a class via an attribute on your controller method which intercepts the call before your method runs.
这些允许您通过控制器方法上的属性安装类,该属性会在方法运行之前拦截调用。 You can do this kind of basic checking here and return a standard error handler result from here.
您可以在此处进行这种基本检查,并从此处返回标准错误处理程序结果。
ASP.NET MVC has a built-in HandleErrorFilterAttribute
that helps you to return error views if some errors occured in action or other filters. ASP.NET MVC具有内置的
HandleErrorFilterAttribute
,如果在操作或其他筛选器中发生某些错误,则可以帮助您返回错误视图。 The built-in HandleError
filter returns view not a partial view so you may have to create a custom one to return a partial view. 内置的
HandleError
过滤器返回的视图不是部分视图,因此您可能必须创建一个自定义视图才能返回部分视图。 The idea is you have to throw some custom exception from your action if fbUID
is null and the custom handle error filter returns a partial view if it handles that exception. 想法是,如果
fbUID
为null,并且自定义句柄错误过滤器处理该异常,则必须从操作中引发一些自定义异常。
I suggest going for a custom handle error filter approach only if you see this functionality in many places else it's more work for a simple thing! 我建议仅当您在许多地方都看到此功能时,才采用自定义句柄错误过滤器方法,否则,对于一件简单的事情,它会更加有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.