none
How to intercept GET requests for static files in a WCF service? RRS feed

  • Question

  • Is there a way to intercept GET requests for static files in WCF services? I want to be able to check whether a user should have access to a file (based on a call to a stored procedure), and if they do, allow the GET request to go through, and if not to redirect them to another web page. Is this possible? The files in question are in a virtual directory stored on another disk (if that matters). Thanks.
    • Moved by Fred Bao Tuesday, July 28, 2015 9:52 AM WCF Related
    Monday, July 20, 2015 11:26 PM

All replies

  • Hi mfearby,

    As your issue is related to WCF, I'll move your post to WCF forum for help.

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 28, 2015 9:52 AM
  • Hi mfearby,

    According to your description, in my opinion is that you want to use WCF Restful Service to redirect a webpage.If user download file then allowed request,if not to redirect them to another web page.

    If so, I suggest you can create a method that can accept 2 params and then redirect the user to the new website. Code shown below:

    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;

    namespace RestRedirection
    {
        [ServiceContract]
        public class Service1
        {
            [OperationContract, WebGet(UriTemplate="ProcessSomething?a={a}&b={b}")]
            public void GetData(string a, string b)
            {
                 if(download)
                {
                }
                else // redirect
               {
                    WebOperationContext.Current.OutgoingResponse.StatusCode =     System.Net.HttpStatusCode.Redirect;
                     WebOperationContext.Current.OutgoingResponse.Headers.Add("Location", "http://www.microsoft.com");
                }
                //process request here
              }
    }

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj



    Wednesday, July 29, 2015 11:39 AM
    Moderator
  • Thanks for your reply. I've copied your suggestion into my web service code but can't get it to work. See comments within the code:

    In my global.asax.cs:
    private void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute("CloudCache", new WebServiceHostFactory(), typeof(CloudCacheService)));
    }
    In CloudCacheService.cs:
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    
    namespace MyNamespace
    {
        [ServiceContract]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
        public class CloudCacheService
        {
            public CloudCacheService() { }
            
            // This is an application in IIS: "/app/Service/"
            // This is an virtual directory:  "/app/Service/CloudCache/" (pointing to its own partition on the web server)
            // The user can GET files via /app/Service/CloudCache/guid/filename.ext
            [OperationContract, WebGet(UriTemplate = "/{FileID}/{FileName}")]
            public void GetData(string FileID, string FileName)
            {
                // This method never gets called, presumably because "CloudCache" is a virtual,
                // so IIS allows access to it immediately and never lets the WCF process it?
                bool allow = false;
    
                if (!allow)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
                    WebOperationContext.Current.OutgoingResponse.Headers.Add("Location", "http://www.microsoft.com");
                }
            }
    
        }
    }


    Wednesday, July 29, 2015 10:27 PM
  • Hi mfearby,

    Ok,may be you can add the following code in your web.config file,code shown below:

    <system.webServer>
        <directoryBrowse enabled="true" />
     </system.webServer>

    This node should be added as a child of the configuration node ( <configuration>).

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj



    Friday, July 31, 2015 1:39 AM
    Moderator
  • That didn't work either, sorry. I'm beginning to think that a WCF service can't intercept requests for static files in a virtual directory that's the same as the routePrefix for a ServiceRoute.
    Monday, August 10, 2015 5:14 AM