How to create an ASP.NET Core class library - asp.net-core

I want to use Visual Studio tooling around razor pages, components, views etc. for a project I am working on.
The scenario is the following: I want to have one main web application which is pluggable, so there shall be no direct reference to the plugins (which are class libraries).
class library should allow for razor pages etc
class library should allow for displaying controllers (that one already works)
What I did so far was changing the project file and changing the SDK to Microsoft.NET.Sdk.Web
You can reproduce this issue with this GitHub-link.
However the project I have changed now has "connected services" and launchSettings.json.
Is that a behavior I can ignore or will the project have side effects with the change I made?

What you're looking for (and what the switch to the SDK did) is a Razor Class Library, or RCL for short. It looks like you might have started with an ASP.NET Core site, which would explain the presence of launchSettings.json. An RCL is similar to an ASP.NET Core site in that you can include most of the same things: controllers, views, Razor pages, view components, tag helpers, static files, etc. However, notably, you will not have a Program.cs, Startup.cs, launchSettings.json, or any configuration files like appsettings.json. This is because, at the end of the day, it's just a library, and not something that runs on its own or stands on its own. You can make use of standard abstractions like IConfiguration/IOptions, ILogger, etc., but the actual implementation of these will come from your app, not the library. You should consult the documentation for more information.

Related

How to inspect asp.net core methods?

I created a new ASP.NET Core MVC application with individual user accounts authentication. I would like to see how they implemented some methods. I suppose I can do it since ASP.NET Core is open source project, but I can't find these methods in github repository.
https://github.com/aspnet/AspNetCore/
I am highly interested in this method.
https://localhost:portnumber/Identity/Account/Login
Q1: How to find this method in my project and is it possible to debug it?
Q2: Why I dont see AccountController file in my new created app?
ANSWER:
It turned out, that from .net core 2.2 version if you want to see or change Identity controllers, you have to scaffold them manually.
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio#scaffold-identity-into-an-empty-project
The methods you are referring to are part of your application. Have a look under the Areas folder. There should be a subfolder called Identity. Under the Identity folder, you should find the AccountController and your Login action method should be inside this controller.
If you really want to look into the source code you need to have a look at the SignInManager.cs class and see how the SignInAsync method is implemented, which is used by the Login action method.
EDIT
Please refer to the screenshot below
EDIT 2
Structure for the newly created app using Web Application template

Is it feasible to convert an single MVC razor view/page into a SPA?

I currently have an ASP.NET Core MVC application with multiple pages with one particular page having a lot of hand-made javascript code and ajax requests. It's becoming quite hard to manage and I would like to move to a framework like Angular, React etc for this particular page only. It is feasible to convert a single MVC view to a SPA app and still keep the other pages? Does anyone have any references on this? Google is not being too helpful...
The simplest way I have found to do this is to use one of the SPA templates and then add the static pages around that.
Here's an an example project I have made that demonstrates the concept by adding a new Razor Page to the angular SPA template.
To do this for an already existing MVC/Razor Pages app I would suggest copying the following from an SPA template project to the MVC/Razor Pages app:
ClientApp
Controllers (SampleDataController.cs)
Views (Home, Shared)
npm_shrinkwrap.json
package.json
tsconfig.json
webpack.config.json
webpack.config.vendor.json
Startup.cs (copy the SPA things)
This is not an exhaustive list but should get someone most of the way.

Streamline mvc 4 project for optimal web api performance

I'm currently reviewing ASP.NET Web API and wondered if anybody has managed to 'streamline' the VS2010/12 project template that you get when you create a new ASP.NET Web API project.
Call me skeptical, but I'm wondering if Microsoft have thrown a few things in the project template that might not be needed, whereby its removal will make the things go faster.
For example, in web forms, you get most of the http modules included by default, and in most cases you won't need all modules.
Basically, I'm looking for the best performance possible for my shiny new Web API webservice, and if I can throw out the spare tyre and the ashtray, I will.
Thanks.
There was a lot of feedback about getting a cleaner starting point for the purist developers. Release notes here:
"Empty Project Template
The MVC Empty project template is now truly empty so that you can
start from a completely clean slate. The earlier version of the Empty
project template has been renamed to Basic."
You will find that the "Empty" project template will be the minimum you can get away with on a new WebApi project.
File -> Templates -> Web - Asp.Net MVC 4 Web Application
Select Template "Empty".

How do I register a controller that has been created in an AREA

I am using MVC4's WEB API to expose a controller.
Initially I created created a MVC4 WEBAPI project, set the project not to open any web page, wait for an external app to call the URL (WEB API). The project in VS2010 is set to run from IIS7 Express, not VS2010's Dev Server. This works OK, the browser prompts me to down load a file. Which is OK for me, as the browser does not know what to do with the returned data (RAW TEXT).
Next, I created an AREA in the MVC4 project area, then added a controller (WEB API type).
Then I once again ran the project and in a browser entered the the URL (WEB API). And it fails.
Ed.
The DefaultHttpControllerFactory doesn't work with Areas by default. To get this functionality you have to write your custom HttpControllerFactory.
You can find how to do this at the post How to create HttpControllerFactory to support Areas
While it is possible to place WebApi Controllers in different Areas, ASP.NET MVC 4 RC will still treat these ApiControllers as if they all reside in the same namespace. This is a limitation of the DefaultHttpControllerSelector, which is responsible for selecting the appropriate ApiController.
Fortunately, you can inject your own implementation of this class. In fact, I've already encountered this very issue and written an "area aware" HttpControllerSelector. You can find a blog post of mine about this issue and its solution here:
http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/

Can we use Razor on an existing ASP.NET 4 website?

Is it possible to use Razor on an existing ASP.NET 4 website?
Yes, you can use Razor with an existing ASP.NET WebSite. Simply open your website using the WebMatrix tool and start adding CSHTML files.
One caveat is that if your website is using WebForms controls the WebMatrix tool will not provide any help working with them in existing aspx pages. Additionally, Razor does not support WebForms so you will not be able to add something like <asp:GridView> to a CSHTML file.
You shouldn't even need to open the site in Web Matrix if you already have VS2010 and MVC 3 (which includes the Visual Studio tools for building ASP.NET Razor websites) installed. Installing MVC 3 makes the libraries required for developing Razor pages available, even to existing web applications.
See:
http://www.asp.net/webmatrix/tutorials/program-asp-net-web-pages-in-visual-studio
You don't need to create a new Web Pages site (as per the instructions). You can just open up an existing web site, right click the site's root folder, click add item and you should see "Web Page (Razor)" as an option.
Inellisense and debugging works in the Razor pages just like the Web Forms pages
As stated above, keep in mind that ASP.Net Web Pages (Razor) and ASP.Net WebForms are really different platforms, and the reusable components of each can not (or at least should not) be used
marcind is correct, if you want to open your existing ASP.NET website in WebMatrix and work on it from within the tool. If, on the other hand (or in addition to), you want to use Razor syntax in your site and stay within VisualStudio, check out this article: http://weblogs.asp.net/rashid/archive/2010/07/10/use-razor-as-asp-net-mvc-viewengine.aspx
There are four things you need to do:
Add References to the Razor assemblies installed with WebMatrix. These can be found at C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies
Create a custom ViewEngine class, a View class that inherits from IView (not that hard, check out the source in the article above)
Add your new ViewEngine in Global.asax Application_Start()
ViewEngines.Engines.Add(new RazorViewEngine(("cs"));
Create your view pages with a .cshtml extension, instead of .aspx
There are a few steps here, but it's quick work, and the source from the article above will get you a long way there.