How do I get the current url in a Razor template served by NancyFX?

Justin Dearing picture Justin Dearing · Jul 25, 2014 · Viewed 8.9k times · Source

According to this answer the way to do it in MVC Razor is @Request.RawUrl, @Request.Url.ToString() or @Request.Url.AbsoluteUri.

On my razor page, ReSharper resolves @Request to using @Nancy, and I can't find an instance of HttpRequestBase.RawUrl.

How do I get the RawUrl from a Nancy served template?

Answer

TheCodeJunkie picture TheCodeJunkie · Jul 25, 2014

Currently you would have to either expose it on your ViewModel or derive your own page base class form the NancyRazorViewBase<TModel> class, and expose it from the RenderContext.Context.Request.Url property

You can see an example of creating your own page base class here https://github.com/NancyFx/Nancy/blob/master/src/Nancy.ViewEngines.Razor.Tests/GreetingViewBase.cs

I just submitted a pull-request with a code change that makes the following possible from your Razor views https://github.com/NancyFx/Nancy/pull/1633

@Request.Url
@Context.Request.Url

As soon as the pull-request has been accepted you will be able to use it by using our bleeding edge builds https://www.myget.org/gallery/nancyfx

It will then be part of the Nancy v1-alpha release on the official Nuget feed, once we release that