Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

razor

nancy

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?

like image 293
Justin Dearing Avatar asked Jul 25 '14 17:07

Justin Dearing


People also ask

How do I get the current URL for my razor?

Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.

What is Razor web page?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.

What is @: In Cshtml?

CSHTML files use the @ symbol and Razor reserved directives to transition from HTML markup to C# code. Most CSHTML files are created in Microsoft Visual Studio. Visual Studio provides syntax highlighting and Intellisense code suggestions that help developers create CSHTML files.


1 Answers

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

like image 137
TheCodeJunkie Avatar answered Oct 17 '22 19:10

TheCodeJunkie