简体   繁体   中英

Why does any reference to Server.MapPath() kill my HttpContext Request.Params[]?

I am very much a novice in HttpHandlers. This is actually my first attempt at it.

I have this Ajax handler that responds to a jQuery ajax call in my web app front end:

public class ajaxMeetingHandler : IHttpHandler {

        public void ProcessRequest(HttpContext context) {

            string resultsJSON = "";

            string requestType = context.Request.Params["requestType"];

            if (!String.IsNullOrEmpty(requestType)) {

                switch (requestType) {

                    case "RecentMXMeetings":

                        resultsJSON = SerialiseRecentMeetings(context, "Maintenance");

                        // SerialiseRecentMeetings() is a method in the class 
                        // that works fine and is not included for brevity.

                        break;

                    // more cases (not included for brevity)
                }
            }

        public bool IsReusable {
            get {
                return false;
            }
        }


    }

}

And this works perfectly.

However, if I add either of these two statements anywhere in the code:

var x = context.Server.MapPath("/");
var y = HttpContext.Current.Server.MapPath("/");

...my Context.Request.Params[] collection becomes null, and IsNullOrEmpty(requestType) now sees requestType as null. In fact, ALL the Request.Params[] are null.

If I comment out those statements (and completely rebuild the solution) the thing goes back to working properly.

In the meantime, I am going to move the calls to MapPath() out to a static "RunTimeEnvironment" class so I can get the path I need from there without touching MapPath() from inside this HttpHandler. Is that a viable or recommended solution?

It turns out my problem was not related to the code itself, per se, but how I was running the project.

Visual Studio, when you click on the "Start Debug" button will start the solution at it's root document (ie, index.html). That is UNLESS the current open document is of a runnable type, such as .html. If your current open window is one of your class files (ie, .cs), it will not attempt to run the class file, but will start the debug session at your root document.

However, it WILL attempt to run a Generic Handler (.ashx) all by itself if that is the document you currently have open. And, by doing so, it was not starting at the index.html page which issues my ajax calls and sends parameters to the Handler. So, my Params collection was null because it was literally null. Running the .ashx by itself supplies no parameters.

So, the reason it worked after changing my call type from GET to POST and Back to GET again is because in doing so, I opened the index.html file to make that change, and when I started my debug session again, my current document was the index.html file, not the Generic Handler .ashx file.

I should probably lose a hundred reputations points just for making this dumb of a mistake. But in case it helps others, there it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM