Let's say my url is " https://www.mywebsite.com/app/company/employees/5 " or " https://www.mywebsite.com/company/employees?id=5&name=jack "
I'm looking for a way to get the "base" path, or whatever it's called. Like the "base" path would be "/app/company/employees" for both, without the "/5" part or "?id=5&name=jack" part.
I was using string.Join("/", request.ApplicationPath, request.RequestContext.RouteData.Values["controller"], request.RequestContext.RouteData.Values["action"])
to get it (request is HttpRequestBase), but it doesn't work the way I want since it includes the Index action too. Like if the Url is " https://www.mywebsite.com/app/company " I want "/app/company/" not "/app/company/Index". I can always check if the action is Index or not but it feels like kind of a "code smell" to me.
Is it even a code smell? Is there any proper way to accomplish this?
The Uri Class offers many properties https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=netframework-4.7.2
Try this
string val = HttpContext.Current.Request.Url.AbsolutePath;
or in your case if your request variable is an HttpRequest object, try:
string val = request.Url.AbsolutePath;
使用HttpContext.Current.Request.Url.AbsolutePath
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.