Creating a .cshtml view within a .Net Standard library - c#

I'm trying to create a shared class library using .Net Standard 2.0 which will be used by both Asp.Net Core MVC (2.1) as well as Asp.Net MVC on .Net Framework 4.5+. This is fine for my model classes and I'm able to resolve all the api's that I require so far. However, I now need to create the shared, partial view that uses the model but I can't seem to find anything useful out there to describe what nuget packages I should reference etc. When I create the .cshtml view I can't resolve #model
I've tried adding the Microsoft.NET.Sdk.Razor nuget package but it didn't work and I feel I'm missing some essential documentation that explains this better. If anyone has any ideas, please point me in the right direction or tell me if this is impossible from a .Net Standard project.
EDIT: Let's forget about .Net Standard. Is it possible to create a razor view that can be used by both .Net Core and .Net Framework?

Related

Referencing .Net Core project from .NET MAUI

I have 2 projects in my solution - .Net Maui(GUI) and ASP.Net Core Web API(API)
After adding a project reference to my GUI I get the following errors:
And I can find very little information on that, although I'm probably looking in wrong places. Can you give a hand with that?
Edit:
To complete/expand my question, I'm looking for a way to structer my solution a little bit better, I don't want to have everything in .NET Maui project.
I was following this tutorial https://www.youtube.com/watch?v=LrZwd-f0M4I
The author does create .NET Core project for API and Db connection
(EF Core) and then he uses the Api in .NET Maui
What am I missing?

How to do custom model binding for Web Api in a .NET Framework web application?

I'm starting to convert a legacy .NET Framework ASP.NET API (which does not use the WebApi framework) into a .NET Core WebApi project. Due to dependencies, as an intermediate step I'm just swapping out the current API framework for WebApi, keeping the project targetting the .NET Framework.
The current version of the API supports binding comma-separated values on the query string into array properties on the model; I want to continue to support this.
So I need some custom model binding. There are plenty of similar questions about this, but they all seem to use .NET Core, different versions of the WebApi framework, and omit the namespaces being used (e.g. ASP.NET Core 1 Web API Model Binding Array).
I'm using .NET Framework 4.7 and version 5.2.7 of various Microsoft.AspNet.WebApi packages. It seems like I need a class that implements IModelBinder, but this interface exists in at least 3 different namespaces.
Exactly what IModelBinder interface do I need to implement, and once I've implemented it, how do I tell the WebApi framework to use my binder for specific properties?
Since you are targeting Web API that is implemented via Microsoft.AspNet.WebApi packages, you need to use types from these packages. Specifically, you need IModelBinder from the namespace System.Web.Http.ModelBinding (it is defined in System.Web.Http.dll provided by Microsoft.AspNet.WebApi.Core package).
Please refer to THIS on how to implement and use binding comma-separated values on the query string.

Cannot find SignedCms class in .NET Core Source Browser anymore

Few days ago I was able to view SignedCms class and other related classes in System.Security.Cryptography.Pkcs namespace through .NET Core Source Browser, but now I cannot find any class from System.Security.Cryptography.Pkcs namespace in .NET Core Source Browser anymore.
Using .NET Core Source Browser it was easy for me to find all references to some member and generally move through code with a need to download anything.
.NET Framework Reference Source still contains SignedCms class, but that implementation is not cross-platform and doesn't interest me.
Doesn't anyone know why this is happening?
Is this maybe because .NET Core FX repository was moved to .NET Runtime repository?
It would also be nice that layout of .NET Core Source Browser is adaptive. When I view it on my iPad, the list view from the left part occupies half of the screen.
I also asked this question in SourceBrowser repository: https://github.com/KirillOsenkov/SourceBrowser/issues/137.
There seems to be some internal problem with building projects from runtime repository after its versions got updated.

What is the difference between the these two FileStreamResult from different classes?

What is the difference between System.Web.Mvc's FileStreamResult and Microsoft.AspNetCore.Mvc's FileStreamResult? Is there any significant take away of using the latter over the first one?
Edit:
Is it possible to have .Net Core project but still use System.Web.Mvc? I don't know how but just asking for possibility here, is it or is not?
The System.Web.Mvc namespace is used the .NET Framework. If your project is targetting the .NET Framework, use this.
The Microsoft.AspNetCore.Mvc namespace is used in .NET Core. If your project is targetting .NET Core, use this.
Only one of them will be available to you, depending on your project.
You can have an ASP.NET Core project that targets the full .NET Framework, and then add a reference to System.Web.Mvc.dll directly. That might work. But I don't know why you would want to.
But if your ASP.NET Core project is targetting .NET Core, then there is no way to use System.Web.Mvc.
System.Web.Mvc namespace is used by the .NET Framework MVC. Microsoft.AspNetCore.Mvc is used by .NET Core MVC. Pick appropriatly for the type of project you are building.

ASP.NET Core reference my object model library

I am building an ASP.NET Core app for WebAPI and I have a regular .NET class library containing my object model. Is there an easy way to reference that in my ASP.NET core app? I tried doing "Add Reference" and pointing to the dll in our release folder (this is how we normally go about adding references) but that gave me an error stating that only .NET assemblies could be referenced. It is a .NET assembly (.NET 4.61 - my ASP.NET Core app is targeting the full framework - also .NET 4.61). I also tried adding the existing project to my solution and referencing it as a project reference. This will add it to my project.json but I get an error that it can't resolve the reference. I did follow this guide and it gave me the same error - could not resolve reference. It doesn't seem like it should be this hard to get this to work. I would imagine that most developers would want to abstract their object model out from a particular web app and be able to reference it. At this point, I'm not using anything crazy. I just have a collection of interfaces using standard .NET types. Though down the road I also want to be able to add references to COM libraries (I'm not exactly confident right now that that is going to work). If anyone has any insight for me, it would be much appreciated.
Thanks.

Categories