Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to get the full server path in a ASP.NET MVC view?

Tags:

asp.net-mvc

I'm new to ASP.NET MVC and I'm trying to get the full url to a action when working in a view. I need this to submit to a third party API as a callback. For example what I need is

http://myserver.com/controller/action

When I use

<%= Url.Action("action", "controller") %>

I get

/controller/action

I know several ways to add the server base-path to this but I'm wondering what is the preferred way to do this in the ASP.NET MVC view?

EDIT: Just to clarify, it's not the URL for the current view/action it's for another action in the same controller.

like image 771
maz Avatar asked Dec 06 '08 22:12

maz


1 Answers

In order to catch variations in the protocol (http / https), diffrent ports and virtual paths (can't always assume we will be in server root) I ended up with the following solution:

<%= Request.Url.GetLeftPart(System.UriPartial.Authority) + Url.Action("action", "controller")%>

I'm working on moving this to a extension method to make it prettier.

like image 192
maz Avatar answered Sep 30 '22 21:09

maz