I am building a Web Application in ASP .NET 5 using Visual Studio 2015. I have created a solution with Data Access, Business, Services and User Interface layers.
I have referenced the Services in User Interface layer.Since in MVC 6 both Web API and MVC fall under the same project template, it is necessary to have two different layers for services and UI or same project with different controllers is enough.?
And also in the UI project I have uncommented the following lines in Startup.cs
services.AddWebApiConventions();
routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
While running the project, the home page comes up fine but when I click on links in the home page the url changes like this
http://localhost:45075/api/Home
And it gives error page saying that
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied.
Sample.Services.Controllers.HomeController.Index
Sample.Services.Controllers.HomeController.About
Sample.Services.Controllers.HomeController.Contact
Sample.Services.Controllers.HomeController.Error
My understanding is that since I enabled Web Api conventions, it is going to the Home Controller of services which has route attribute
[Route("api/[controller]")]
If so how resolve this conflict by specifying namespace or some constraints while rendering views in the routes.
I am a beginner. Correct me if I am wrong.
On MVC 6 you can have RESTful APIs as long as Views in the same project and even on a same controller.
One ways to set up routing for you application is to create a map on Startup.cs like this, with IApplicationBuilder:
Then, you can specify the routes for each action:
By setting a [HttpGet] (or any other HTTP verb) on the Action without parameters, it will create a route by convention, following you action's name. That way, you can solve many conflicts. It's also possible to put multiple actions of a same HTTP verb on one Controller.
And as stated above, please ask only one question per post.
Related
I'm creating an application with a .Net Web Api project wanting to use pure AngularJS as the client side. Since Web Api is built on top of MVC, it creates MVC specific and default items that I feel is not needed. These items include the HomeController, _ViewStart.cshtml, _layout.cshtml, etc. I tried removing them but it comes up with errors. Has anyone tried to remove the MVC stuff out of the web api project and used separate client side front-end? Is it even possible to remove the MVC items without errors?
Remove RouteConfig.cs from App_Start, remove the Views directory and all sub-directories including the Views internal web.config file. Comment out or delete all the lines in the Global.asax.cs Application_Start method except GlobalConfiguration.Configure(WebApiConfig.Register). Remove the HomeController, add an index.html and any needed Angular scripts and go at it. I also added solution folders to organize my views as reusing the existing Views folders did not work. I'm using VS 2015 but is should work for 2013 also. PWE
Web API is not built on top of MVC.
The default templates bring in MVC for the sake of supporting a help page, but you don't need to use it.
You can start with an empty web project and just check Web API.
The routing piece is server routing and it's part of what maps the URL to Controllers+Actions, it has nothing to do with Angular routing.
As Mike Cheel alluded to, there are no dependencies between MVC and Web API. However, if you use the built-in templates, it's easy to get the impression that the 2 are linked. They include a lot of stuff in these templates because they can't anticipate where you want to go with your project... so they try to cover all the bases.
For your purposes, you would probably be better off to start with an empty project and add only the components that you actually need. For this approach, some of the best tutorials and starter projects are from Taiseer Joudeh's "Bit of Technology" blog. His tutorials helped me to build an "MVC Free" web application from scratch that uses JSON Web Tokens and AngularJS Interceptors for security and Web API 2 and Entity Framework to serve up the data.
He has many tutorials on his website... but you might want to start with "AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity". What what.. you didn't ask about security? Well... security is an issue that you will need to confront at some point anyway... and Taiseer presents a nice solution for securing an Angular/Web API application.
So I am totally new to the Web API and I thought to myself I would start a test project to learn it and AngularJS and how they can work together nicely ... etc.
I have created a multi-tier architecture within my solution, and inside my TPlan.API project, I have the following route configured in the Global.asax
GlobalConfiguration.Configuration.Routes.Add("default",
new HttpRoute("api/{controller}"));
TPlan.WEB is my MVC application, and it is set up as "Start Up Project". When I run it, I want to be able to go to:
mysite:port/api/test
And get the value from the API from my test controller in there.
However it is not working for me, I get the following:
No HTTP resource was found that matches the request URI 'mysite:port/api/test'.
Er, in visual studio, right click on the solution and go to properties.
Go to startup and select the "multiple projects" option and tick the website and the webservice to both start up.
That will start both of them. But as has been pointed out, they're running on separate ports, they're completely separate applications...
I don't think you can register a route that belongs outside of the website.
We work like this
View => POST / GET => MVC Controller => HTTP Request => API Controller
So our mvc views post or get to our mvc controllers, and then we fire off a separate http request to the web api. this is a server to server call, its like calling any external web service. we wait for the response from the api and then return whatever is required to the view...
What you are attempting isn't logically possible without installing your WebAPI project into IIS ahead of time, which I'm sure is not what you want. Both projects cannot be run at the same time, as only one project will be able to launch a debug session of IIS Express. Even if both projects were able to run at the same time, they would be on different logical ports, and routing from one project would have to be manually sent to the listening port of the other instance. Basically, your Global.asax on your API project will never run, as that project will be built as a class library.
Make your TPlan.API project a simple Assembly, reference it from TPlan.Web and pass the GlobalConfiguration.Configuration property over to a Register method that is in your API assembly. When the MVC project starts, both the MVC routes and the Web Api routes will be active on the same web host.
Here is an example of an API that has both a console host and a web host in the same solution.
Please check the following site. I believe the issue lies in the configuration of the route
http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api
I've watched a lot of video tutorials and read even more articles. I don't think I've seen one that answers this question and I'm stuck. :S
I created an MVC4 Application with Web API. Going off of the tutorials I've read I went with a standard naming convention. Say for example, /api/user and /api/user/id, etc.
Now I would like to create CRUD pages for the User entity. Of course, these pages would fit well under the /Admin/ folder, but I can't create a controller that generates views of /Admin/User/Create, /Admin/User/View, etc., since this would require creating a controller called UserController - but this is already created for the WebAPI.
Doesn't it seem standard or at least 'ok' to create CRUD pages for my Entities with this folder structure:
Views/Admin/User/
Views/Admin/Product/
etc, etc.
This allows each entity/table to have its own folder with its own Create.cshtml, Index.cshtml, etc.
But again, I have already created a WebAPI with this structure:
api/User/
api/Product/
So, now I want to create some Admin pages to manage the database. When I create a MVC controller with read/write using Entity Framework I can't figure out what to name the controller to give me the URI structure I'm wanting.
Of course, I don't really care what the controllers are called, but I don't know what steps are required to get the URI structure. For example, I created an MVC controller and named it AdminProduct, but now I have to go to /AdminProduct/ to see those pages.
Can someone point me?
You can have two controllers with the same name as long as they are in different namespaces. For example, I add all of my System.Web.Http.ApiController classes under the /Controllers/Api folder in my project. By default, Visual Studio will include these controllers in a namespace called <ProjectName>.Controllers.Api. Then, inside the /Controllers folder you can add System.Web.Mvc.Controller classes there, and they will be in the <ProjectName>.Controllers namespace. So, long-story-short, you can have an MVC controller named UserController and a WebApi controller named UserController, and there is no conflict.
The views for your MVC controller actions, like /User/Create and /User/Delete should be put under the /Views/User/ folder of your project - each with the same name as the action in the UserController. As far as having admin pages, why not create an AdminController or using the [Authorize] action filter on those actions you want secured?
I'm not sure I understood your entire problem. ASP.NET MVC binds controller names and actions to views by this convention.
I'm developing an ASP.MVC 3 project on my local computer where it is located at the root of the local web server.
localhost:12345/(project is here)
However, when I deploy to our public web server this application will be located in a subfolder
www.mycompany.com/myapp/(project goes here)
How do I deal with that mismatch? A few questions come to mind:
Do I need to adjust my MVC routes? Or will they just capture anything after /myapp/ ?
Do I use HomeController when I don't really want 'Home' to appear in the route? i.e. /myapp/home/(action)/(id) - rather I want this: /myapp/(action)/(id) if Home is the controller.
Should I match this folder structure on my local machine? (This project will never have access to anything outside that 'myapp' folder)
If 3 is yes, how?
I'm using VS 2010 with IIS Express locally.
The routes are relative to the web application. Which means that you don't need to have /myapp/ in your routes.
No you don't need to have Home appear in the route. Personally as a practice I take out the default route.
No you don't need to match this folder structure.
In our experience I found that within the Controllers there is no problem with the routes.
However, if you use HTML helpers within your views, like Html.BeginForm or Html.Action, these do not work with the overloaded methods that receive controller, action arguments. You need to put the whole Url in the Html helpers using Url.Content,
This does not work:
Html.BeginForm()
or
Html.BeginForm("ResetPassword", "Account")
But this works:
Html.BeginForm(Url.Content("~/Account/ResetPassword");
i have a solution with the following two projects - MyNamespace.Services and MyNamespace.Web.
Web contains a MVC web application.
In the Service project i have a EmailService class that takes care of sending out emails to the user.
I want to use either a partial or a view (ascx or aspx) for email templates.
I have found several solutions on how to render a partial view and get the result as a string which works fine if the template is inside the web project (as it is a controller in the web project that calls the email service).
(the 2 methods i am trying to use is either http://developersisland.blogspot.com/2009/01/renderpartial-to-string-in-aspnet-mvc.html (at the bottom of the blog) or http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/)
But my email templates are located in the Services project.
How can i refference the path to the templates (partial/view) in my Service project from inside the Web project, that works with either LoadControl or RenderPartial which both takes a virtual path as a parameter ?
It seems like no matter what i do the root directory is set to the Web projects directory.
Is it possible ?
Would be nice to be able to make it work independently of the web project somehow.
I don't think this is possible without developing your own view engine. The default view engine will only look in certain locations for the partial view -- which includes the current view folder and the shared views folder. I don't think you can search for views outside the current project since those views aren't registered with the view engine.
You can consider just creating your HTML helpers to render emails and return it as a string.
Doesn't really matter whether it is partial view or a method returning a string with HTML. i actually think that for your case helper methods would be a better choice.
A simple helper method is also more flexible in the ways you can use it.
You could try creating a custom view engine locator or virtual path provider. Here are a few examples that may help you get going:
Views in seperate assemblies in ASP.NET MVC
Grouping Controllers with ASP.NET MVC
How to use virtual path providers to dynamically load and compile content from virtual paths in ASP.NET 2.0
All of the links above are good, this might help as well. you will certainly be able to get it to find and use the views. The problem I had was in working with them, there was no code completion etc in the other projects. It was semi possible to get that as well by fiddling around with the project file but to be honest I ended up going with the Grouping solution above
Plug in architecture for ASP.NET MVC