.NET Core Web Api cannot access the models folder - .net-core

I have two project in my solution. MVC and WebAPI. I added the WebAPI project later. I added new controller and Models folder in WebAPI project. But I can't create a new instance of my model in the controller. I get "The type or namespace name 'Category' could not be found..". It also doesn't allow me to bind the name using.MyProjectAPI.Models.
What should I do?

Right-click on Reference and click Add Project Reference

Related

Use asp.net authorize in .net core

i want to know asp.net authorize function can it be used in .net core?
Or something can instead in authorize for .net core? thanks
You can use Microsoft.AspNetCore.Identity with Identity Server 4 to authorize your application.
The tutorial from MS is Here.
In short, you can change Authentication to Individual User Accounts when create new projects. Or you can scaffold Identity into an exist project
For example, you can scaffold Identity into an empty project:
From Solution Explorer, right-click on the project > Add > New Scaffolded Item.
From the left pane of the Add New Scaffolded Item dialog, select Identity > Add.
In the Add Identity dialog, select the options you want.
Select your existing layout page, or your layout file will be overwritten with incorrect markup:
~/Pages/Shared/_Layout.cshtml for Razor Pages
~/Views/Shared/_Layout.cshtml for MVC projects
Blazor Server apps created from the Blazor Server template (blazorserver) aren't configured for Razor Pages or MVC by default. Leave the layout page entry blank.
Select the + button to create a new Data context class. Accept the default value or specify a class (for example, MyApplication.Data.ApplicationDbContext).
Select Add.
Then generate Identity database schema:
Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Add-Migration CreateIdentitySchema
Update-Database

What is a best way to share controler with MVC Views to another project

In the asp.net core projects:
What is the best way to share a controller with MVC Views to another project?
Is it any way to build a controller with Views as a component in the assembly and share it to the different projects.
In other words how to not just simply copy code and how to reuse it to another project.
Yes its a new feature in ASP Core called Application Parts.
It allows you to store your ASP Core Controllers/Views etc in a separate assembly and then you can include/share it as Application Parts/Dlls which is new in ASP Core, the sample code from MSDN is linked above for ASP Core.
In the ConfigureServices method of your Startup class just add this:
// set this up
services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
Where assembly is the name of your instance Assembly with your controllers & Services, then you can load it either getting it from any included type or like this:
var assembly = Assembly.Load("YourApp.NameSace.AssemblyName");
Another nice ref implementation and explanation

How to add a MVC Account controller to WebApi project in Visual Studio 2013?

I am trying to understand the concept of one asp.net. I did the entire exercise here.
Its easy.
Now trying to understand further, I created a new Asp.Net project and selected Web API template as follows. So we see that MVC and Web API check boxes are selected.
If I observe the project The AccountController is an ApiController and so the views folder does not have any Account and Manage folders as expected.
Now my question is how do I add an Mvc Account controller to the above project? I very well know that if I select MVC in the new project dialog as follows I can get that.
What I want is to add is an MVC account controller along with the views exactly like the following in the Web api project show earlier.
This should be very easy I guess. So could someone please describe some steps? Like for example, which model and context we need to select on add scaffold and then Add Controller dialogs?
In other words I want to understand looking at a controller say AccountController be it Api or Mvc, how do I understand what is the model class and Data context associated with it.
The plenty of scafold templates that exists are very useful to have an application running. Even you can create a new WepApplication project and add the Nuget package AspNet.Identity Sample, wich has a lot of features.
However for your project, I recommend you to start with and Empty Web Api project and then start adding only the stuff that you need. This way you will understand better how your Wep Api works with AspNet Identity (Account Management, External Login Providers like Google or Facebook, ...).
This Step by Step tutorial (and the next series) will guide you in implementing and understanding the Web Api and AspNet Identity starting from an Empty Web Api project.
Hope this helps.

combine asp net projects and mvc3 in one single solution

I am facing a problem with adding two projects in the same solution and both run as a single instance.
Let me explain that in more details. I have MVC project which is the main web application I used in my project now I want to add a new asp.net application to use for rendering reports and get the benefit of using asp.net page with Report Viewer.So, I was thinking in adding a new asp.net application that will contains my reports then map this new pages to my mvc application
The problem is how to deploy these two instances as one application in server.
I know that i can run both applications in debug mode by using debug > start new instance but how i can manage that in server.
Also is there is a way to deal with those two applications as one application
Update:
Using Ihor idea and with the help of this tutorials
How can I use a reportviewer control in an asp.net mvc 3 razor view?
http://msdn.microsoft.com/en-us/library/ms251692%28v=vs.100%29.aspx
I am facing a problem in binding to my object datasource attached to project. For example I have repository for SalesInvoices and I want to use GetData function exists in that repository to bind report with
ASP.NET MVC is built on regular ASP.NET Web Forms. In your scenario you don't need to have a specific different project for Reports, you can have Reports inside your MVC project. For example you can add a new GenericReport.aspx under some folder (for example "Reports") of your MVC project.
Then when user requests some action on your site you can use PartialView with iframe to your GenericReport.aspx. On this page you can add any webforms-specific logic that you need.
To be honest I am not aware of such particular behaviour of reports, but according to the reply: How can I use a reportviewer control in an asp.net mvc 3 razor view? you will probably need to move your data to a separate project. It shouldn't be web application project but a class library (generating .dll) and you can use it in your MVC project.
Using Ihor idea and with the help of this tutorials
How can I use a reportviewer control in an asp.net mvc 3 razor view?
http://msdn.microsoft.com/en-us/library/ms251692%28v=vs.100%29.aspx
I was facing a problem as I noted in updates in original post I solved by adding a class in reports folder contains method with my model Like that
public class ReportModels
{
public List<SalesInvoice> GetData()
{
return null;
}
}
with this way i can map this model to report successfully

add existing entity framework to new asp.net app

I have an entity framework model that I created in one app, and I'd like to import(?) it into a new asp.net app. How is this done?
If you use a, for exemple, a Class Libary project for to make your EF model you can to add this project to another with "Project Reference"
I like to keep my EF models in their own Class Library projects. Then you can just open the existing project in any new solution in which you want the model to be included.

Resources