How do I register a controller that has been created in an AREA - asp.net-mvc-4

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/

Related

Blazor WebAssembly - How can I add a new *.razor page that don't need to recompile or rebuild the project

I am developing a survey project which collects data from the client with Blazor WebAssemly for the Client-side. Each survey form will have a different design and edit-checked rule. So I decide when I create a new survey form, the application will generate a new *.razor page automatically, for example, ClientSurvey.razor page. Everything works well until I navigate the web application to the ClientSurvey.razor I just have created. The system shows error:
404 Not found.
I have to rebuild the project again, and it can show the ClientSurvey.razor page on my website.
With ASP.NET MVC before I don't have to rebuild the project when I add a new aspx page. Does it still work with Blazor WebAssembly? If yes, how can I change the razor page, and it can update on my website automatically without rebuild or recompile?
And I also meet a problem that when I deploy the project in IIS, I don't see where the UI (.*razor) pages are stored, I know they are compiled to dll. How can I keep them in raw files in the same way with ASP.NET work? With ASP.NET, when I deploy it in IIS, there is always a folder to store aspx pages.
Updated: please see the figure to more clearly

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

how to host webapplication and webapi in single solution

I created a solution with two webapplication project. One is asp.net mvc and second one is web api. Now I want to publish. Kindly suggest me how to publish my webapplication and webapi
all mvc pages, web api controllers and aspx pages can be added to single project, no need to different project, if you need to seperate business you can use areas and class libraries
Process 1 :
You don't need to create separate project for Web API and MVC application in a single solution. You can create a MVC application and add an WebApiConfig.cs (route for api) file in App_Start folder. Write the route for API,(you just need to copy your route from Web API project and paste here). Now you can add/keep your all Api controllers in Controller folder which must be inherited from ApiController. That's it!
Process 2 :
But if you want to keep in separate project in a single solution,
then right click on your solution -> click on properties -> It will open property page. Here select the Multiple Startup projects radio button -> Then you can set both MVC and Web API application as start in action column -> Click on Apply -> Then Ok.
Suppose your Domain name is example.com. Then you can publish your MVC application there. And create a sub-domain, for example, myapi.example.com, and publish your API there. Then enable CORS on API project.
I would recommend to go with Process 1.
If you want to know more about how to publish both project from a single solution, please check this link

MVC4 Web API - OK to delete all views/javascript?

Just created a new MVC4 Web API project - however I don't want any client-side for this, just the back-end to be referenced from other clients.
Is there any, sensible, reason that the default project template includes all of the javascript, views, etc?
Can they be removed without issue?
Default the web api project also contains the regular mvc stuff. If you do not intent to use the web api help pages (https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage) or intend to develop a website part next to your api you can easily remove all views, scripts & statics.

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".