-1

I have an url as below:

http://localhost:100001/foldername/controllername/actionmethodname/?querystring=1&querystring=true

I just want to extract the url without the action method name and querystring.

Required output will be: http://localhost:100001/foldername/controllername

3
  • stackoverflow.com/questions/4630249/get-url-without-querystring this is what you need Commented Jun 8, 2016 at 8:23
  • the post extracts the url without querystring. But I want the url without action method and querystring. Action method should not be included in the url. Is there any one line of code for that? Commented Jun 8, 2016 at 8:46
  • i will add it as an answer Commented Jun 8, 2016 at 8:47

1 Answer 1

2

Answer I came up while finding related answers :

Get the action name first and replace the action name in path with blank

 string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
 string path = Request.Url.GetLeftPart(UriPartial.Path).Replace(actionName, String.Empty);

P.S I didn't copy this answer. I also search how to get the action name value from current request

Sign up to request clarification or add additional context in comments.

4 Comments

I am doing this change on the Layout page. ControllerContext will not exist on the layout, is there any other way to get the last fragment of the url without querystring and replace it with empty string
@Rama not all the time one-liner is a fancy solution :) consider also the hours you spend for this problem. Additional question, in which part you will put this code? in the view, controller or model?
This has worked for me: Request.Url.GetLeftPart(UriPartial.Path).Replace(Request.Url.Segments.Last(), string.Empty)
@Rama yes that is the same concept with my answer but this is from the controller view. Nice catch. I didn't made my answer as one liner because it will look very bad in screen. Need to chop it in pieces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.