ASP.Net Core Replacement for VirtualPathUtility - c#

Is there a replacement for VirtualPathUtility.ToAbsolute in ASP.Net Core? It doesn't seem to be available.
I'm wanting to convert a relative path e.g. "~/bob" into an absolute path, e.g. "/app/bob".
I'm trying to do this from a class library and so doesn't have access to the usual properties of controllers and views.
I'm porting an app from ASP.Net to ASP.Net Core and this library is being called from lots of places and passing parameters through from the controller is not something that I want to be doing - it would be hours of work due to number of places I would have to pass everything through.
I'm using ASP.Net Core 2.1, running on .Net Framework 4.7.1 (although I plan to port to .Net Core in a future release, once some dependencies have been removed so I want a solution that will work with Famework and .Net Core)

You can use HttpRequest.PathBase:
string relativeUrl = "~/foo";
string absoluteUrl = httpContext.Request.PathBase + relative[1..];

Related

System.Web.Hosting.HostingEnvironment in .NET Core?

We have a system that we're moving from .NET Framework to .NET Core.
One piece of this is a logging system that we configure at startup using SimpleInjector. So in
App_Start\SimpleInjectorConfig we have:
private static void InitializeContainer(Container container)
{
var application = System.Web.Hosting.HostingEnvironment.SiteName;
var instance = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
// .. use application and instance in configuring the logging system
}
And the problem, of course, is that in .NET Core there isn't any System.Web.Hosting.
I've been browsing around, and I haven't found a way of getting an equivalent to HostingEnvironment.SiteName in .NET Core - at startup, before any endpoints are active.
Any ideas?
Check the discussion around SiteName on these two .net Core Git tickets. and that will help you to take the decision weather it is really required.
github.com/dotnet/aspnetcore/issues/7400 and
github.com/dotnet/aspnetcore/issues/17069

How to get compatibility version in MVC

When configuring services within MVC app you can set the compatibility version:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
How can I retrieve this version later in my own code to determine which compatibility version is in use?
There does not appear to be a corresponding GetCompatibilityVersion method anywhere and google/stackoverflow search was not my friend.
Any help appreciated.
The MVC compatibility version is actually stored in an instance of a class called MvcCompatibilityOptions. You can retrieve this object by locating it through the IoC container that is being used by the application – either ASP.NET Core's built-in one or a third party one.
For example, with the default IoC, you can retrieve it like this:
var compatibilityVersion = app.ApplicationServices.GetService<IOptions<MvcCompatibilityOptions>>().Value.CompatibilityVersion;
app is an instance of IApplicationBuilder.

How to use openui5 in my asp.net core 2.0 project

I'm building a backend application with asp.net core 2.0, backed by a mssql database.
To speed things up I want to use the openui5-library https://openui5.hana.ondemand.com/
I created the database-model and have all my HTTP-actions saved as controllers to access the data with JSON-Models.
My question is how do I display the "webapp/index.html" when i start the application (routing etc.) and is it generally possible to use openui5 in a asp.net core 2.0 application?
Thanks :P
You can download the openui5 runtime from openui5.org and load it locally and mention the sap.ui.core.js path in index.html.
<script src="./resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.m,sap.ui.layout"
data-sap-ui-theme="sap_belize"
>

Generate web page from Database using AllowHtml in Asp.Net Core 2.0

I have an application use Asp.Net 4.61 in which I read html from a database field Description and use it to populate my a page in my application. I do this by marking the property Description with [AllowHtml].
In my Asp.Net Core 2.0 app, I get an error that the assembly reference or directive is not found.
I have 2 questions - that will hopefully enable me to better answer questions such as this in the future:
Is there a document/site that I could search to see if AllowHtml is
in Core 2.0
Is there a better/more secure way that I should use if I want to
populate web pages by reading from my database than decorating the field/property with [AllowHtml]?
You don't need [AllowHtml] anymore, because nobody denies HTML in ASP.NET Core 2.0:
Don't need [AllowHtml] or RequestValidationEnabled because we don't have request validation in this system
Instead, encode the output and Prevent Cross-Site Scripting

ASP.NET Web API with Linq2Couchbase dependancy injection

I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?
Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.

Categories

Resources