-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

Rama
  • 73
  • 1
  • 4
  • 14

1 Answers1

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

Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35
  • 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 Jun 08 '16 at 09:11
  • @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? – Anonymous Duck Jun 08 '16 at 09:13
  • 1
    This has worked for me: Request.Url.GetLeftPart(UriPartial.Path).Replace(Request.Url.Segments.Last(), string.Empty) – Rama Jun 08 '16 at 09:26
  • @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 – Anonymous Duck Jun 08 '16 at 09:29