标签:style blog http io ar color os 使用 sp
最近在研究office在线预览,用到mvc4 apicontroller 需要传参是文件名,如test.docx导致错误“指定的目录或文件在 Web 服务器上不存在”,
请求的路径如:api/wopi/files/test.docx?access_token=access_token。如下截图:
项目中路由配置:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { //office web apps config.Routes.MapHttpRoute( name: "Contents", routeTemplate: "wopi/files/{name}/contents", defaults: new { controller = "files", action = "GetFile" } ); config.Routes.MapHttpRoute( name: "FileInfo", routeTemplate: "api/wopi/files/{name}", defaults: new { controller = "files", action = "GetFileInfo", name = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // 取消注释下面的代码行可对具有 IQueryable 或 IQueryable<T> 返回类型的操作启用查询支持。 // 若要避免处理意外查询或恶意查询,请使用 QueryableAttribute 上的验证设置来验证传入查询。 // 有关详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=279712。 //config.EnableQuerySupport(); } }
控制器方法,注意这里是apicontroller
public class filesController : ApiController { public CheckFileInfo GetFileInfo(string name, string access_token) { string _access_token = access_token; var file = HostingEnvironment.MapPath("~/App_Data/" + name);//从硬盘中获取name文件 FileInfo info = new FileInfo(file); if (!info.Exists) return null;//不存在返回 var hasher = SHA256.Create(); byte[] hashValue; using (Stream s = File.OpenRead(file)) { hashValue = hasher.ComputeHash(s); } string sha256 = Convert.ToBase64String(hashValue); var json = new CheckFileInfo { BaseFileName = info.Name,//"test.docx", OwnerId = "admin", Size = info.Length, SHA256 = "+17lwXXN0TMwtVJVs4Ll+gDHEIO06l+hXK6zWTUiYms=", Version = "GIYDCMRNGEYC2MJREAZDCORQGA5DKNZOGIZTQMBQGAVTAMB2GAYA====" }; return json; } public HttpResponseMessage GetFile(string name, string access_token) { try { string _access_token = access_token; var file = HostingEnvironment.MapPath("~/App_Data/" + name);//name是文件名 var rv = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(file, FileMode.Open, FileAccess.Read); rv.Content = new StreamContent(stream); rv.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return rv; } catch (Exception ex) { var rv = new HttpResponseMessage(HttpStatusCode.InternalServerError); var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(ex.Message ?? "")); rv.Content = new StreamContent(stream); return rv; } } }
在webconfig中节点system.webserver节点下添加节点<modules runAllManagedModulesForAllRequests="true" />,并将iis模式设置成集成模式(iis->应用程序池->找到你的网站右键“高级设置”,设置成集成模式)。
因为只有iis在集成模式下,system.webserver的设置才会生效。
<system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>
参考资源:
博问:http://q.cnblogs.com/q/54613/
如何:为 IIS 7.0 配置 <system.webServer> 节:http://msdn.microsoft.com/zh-cn/library/bb763179.aspx
IIS配置.html的映射问题:http://blog.csdn.net/songz210/article/details/3216101
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/lipanpan/p/4147244.html