I'm writing a catch-all action method for a 404 page that takes the requested URL, extracts the wordy bits with a regex (\w+)*
and sends those bits, space-separated, to the search engine to generate a list of suggested pages.
The problem is, ServerVariables["UNENCODED_URL"]
appears to be unavailable here in the Action method. Other server variables are available, however.
I could use ServerVariables["URL"]
in conjunction with ServerVariables["QUERY_STRING"]
, both of which are available. But in some situations this leaves out parts of the URL that might contain valid info regarding the user's intention.
For example, if the mal-formed URL is
/test/007/words/something-with-a-dot.xyz/these-words-are-neglected/?stuff-in-querystring
Then the "URL" and "QUERY_STRING" are found as "/test/007/words/something-with-a-dot.xyz"
and "stuff-in-querystring"
"URL" ignores rather bluntly everything after what looks like a file extension. So it doesn't suffice.
So 开发者_如何学Gowhat's up with UNENCODED_URL?
And how can I get its equivalent?Have you tried the HTTP_URL variable? Looks like it returns the raw, encoded URL, for example, "/vdir/default.asp?querystring" - from MSDN.
Source
Also - if you are in a controller's action method, you could(should?) use the HttpContext in place of the SERVER[someProperty]. Something like:
var Url = HttpContext.Request.RawUrl;
which returns the full URL of the current request.
精彩评论