sharepoint中的C#异常处理确保找不到文件
这是我的代码:sharepoint中的C#异常处理确保找不到文件,c#,file,sharepoint,http-status-code-404,http-response-codes,C#,File,Sharepoint,Http Status Code 404,Http Response Codes,这是我的代码: try{ using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteURL)) { #region get the name of the file and check if the file is valid
try{
using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteURL))
{
#region get the name of the file and check if the file is valid
context.AuthenticationMode = Microsoft.SharePoint.Client.ClientAuthenticationMode.FormsAuthentication;
Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo formsAuthInfo = new
Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo(UserName, Password);
context.FormsAuthenticationLoginInfo = formsAuthInfo;
File file = context.Web.GetFileByServerRelativeUrl(relativeFilePath);
context.Load(file);
context.ExecuteQuery();
documentName = Convert.ToString(file.Name);
#endregion
}
}
catch(ServerUnauthorizedAccessexception ex)
{
}
catch(WebException We)
{
}
catch (ServerException s)
{
if (s.Message == "File Not Found.")
{
htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('The specified file is not found in Sharepoint.');self.close();</script></body></html>";
}
else
{
htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('Unable to retrieve file.Please contact your administrator');self.close();</script></body></html>";
}
httpContext.Response.Write(htmlClose);
httpContext.Response.Flush();
}
我使用了不同的捕获块进行捕获:ServerUnauthorizedAccessexception、WebException和server exception。
我发现服务器异常用于确保在sharepoint中找不到文件。
在这部分代码中,我已经完成了
catch (ServerException s)
{
if (s.Message == "File Not Found.")
{
htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('The specified file is not found in Sharepoint.');self.close();</script></body></html>";
}
else
{
htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('Unable to retrieve file.Please contact your administrator');self.close();</script></body></html>";
}
httpContext.Response.Write(htmlClose);
httpContext.Response.Flush();
}
catch(服务器异常)
{
如果(s.Message==“未找到文件”。)
{
htmlClose=“无法获取Filealert('在Sharepoint中找不到指定的文件');self.close();”;
}
其他的
{
htmlClose=“无法获取Filealert('无法检索文件。请与管理员联系”);self.close();”;
}
httpContext.Response.Write(htmlClose);
httpContext.Response.Flush();
}
当您收到WebException
时,您可以使用该属性从web服务器访问响应(如果有)。然后,您可以将其转换为适当的子类,并检查错误代码:
catch (WebException e)
{
var response = (HttpWebResponse) e.Response;
if (response != null && response.StatusCode == HttpStatusCode.NotFound)
{
// You got a 404...
}
}
您当前的代码是什么?我不太明白这个问题。永远不要像那样使用
Message
属性。这意味着它是一条人类可读的消息,任何抛出异常的人都有权随时更改消息。对于丢失的文件,他必须处理sharePoint server异常并使用其内容(可能是类型、代码、消息…)@NicolasR:老实说,sharePoint部分我不清楚。。。我希望这个答案能提供足够的信息来帮助OP。我能这样做吗:catch(ServerException s){if(s.Message==“File Not Found.”{htmlClose=“cannot get File”;}否则{htmlClose=“无法获取Filealert('无法检索文件。请与管理员联系”);self.close();“;}httpContext.Response.Write(htmlClose);httpContext.Response.Flush();}@JonSkeet:是的,这是一个很好的开始,这就是我刚才添加注释的原因。@siva:试试看(并添加断点,以查看异常是否捕获,以及服务器异常的“s”的内容是什么)
catch (WebException e)
{
var response = (HttpWebResponse) e.Response;
if (response != null && response.StatusCode == HttpStatusCode.NotFound)
{
// You got a 404...
}
}