MVC4 routing parameter with point filename suffix causes an error

Source: Internet
Author: User

Error description

Recently in the study of Office Online previews, the use of Mvc4 Apicontroller requires that the parameter is a file name, such as Test.docx causing the error "The specified directory or file does not exist on the WEB server",

The requested path, such as: Api/wopi/files/test.docx?access_token=access_token. As follows:

Route configuration in Project:

 Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {//Office Web AppsCONFIG. 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}); //uncomment the following line of code to enable query support for operations that have a IQueryable or iqueryable<t> return type. //to avoid handling unexpected queries or malicious queries, use the validation settings on Queryableattribute to validate incoming queries. //For more information, please visithttp://go.microsoft.com/fwlink/?LinkId=279712. //CONFIG. Enablequerysupport ();        }    }

Controller method, note that this is apicontroller.

 Public classFilescontroller:apicontroller { PublicCheckfileinfo GetFileInfo (stringNamestringAccess_token) {            string_access_token =Access_token; varFile = Hostingenvironment.mappath ("~/app_data/"+ name);//get the name file from the hard diskFileInfo Info=NewFileInfo (file); if(!info. Exists)return NULL;//there is no return            varHasher =SHA256.            Create (); byte[] hashValue; using(Stream s =File.openread (File)) {HashValue=Hasher.computehash (s); }            stringSHA256 =convert.tobase64string (HashValue); varJSON =NewCheckfileinfo {basefilename= info. Name,//"Test.docx",ownerID="Admin", Size=info. Length, SHA256="+17lwxxn0tmwtvjvs4ll+gdheio06l+hxk6zwtuiyms=", Version="giydcmrngeyc2mjreazdcorqga5dknzogiztqmbqgavtamb2gaya===="            }; returnJSON; }         PublicHttpresponsemessage GetFile (stringNamestringAccess_token) {            Try            {                string_access_token =Access_token; varFile = Hostingenvironment.mappath ("~/app_data/"+ name);//name is the file name                varRV =Newhttpresponsemessage (Httpstatuscode.ok); varstream =NewFileStream (file, FileMode.Open, FileAccess.Read); Rv. Content=NewStreamcontent (stream); Rv. Content.Headers.ContentType=NewMediatypeheadervalue ("Application/octet-stream"); returnRV; }            Catch(Exception ex) {varRV =Newhttpresponsemessage (Httpstatuscode.internalservererror); varstream =NewMemoryStream (UTF8Encoding.Default.GetBytes (ex. Message??"")); Rv. Content=NewStreamcontent (stream); returnRV; }        }    }
Solutions

Add node <modules runallmanagedmodulesforallrequests= "true"/> under node system.webserver node in Webconfig; and set the IIS mode to Integrated mode (iis-> application pool, find your website right-click "Advanced Settings", set into integrated mode).

The system.webserver setting takes effect only if IIS is in Integrated mode.

<system.webServer> "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"/> "true"/> </system.webServer>

Reference resources:

Bo Q: http://q.cnblogs.com/q/54613/

How to: Configure <system.webServer> sections for IIS 7.0: http://msdn.microsoft.com/zh-cn/library/bb763179.aspx
IIS configuration. HTML Mapping Problem: HTTP://BLOG.CSDN.NET/SONGZ210/ARTICLE/DETAILS/3216101

MVC4 routing parameter with point filename suffix causes an error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.