Is there something special about a MVC controller called properties? - c#

After trying to work this out for ages, thinking about routing conflict and more - I started a separate project from the start.
It looks like an MVC controller called "properties" always returns a 403.14 forbidden message when you try to access the root site (http://site/properties) - however, other pages work (http://site/properties/index).
It works fine as a controller in an area, but, I just can't create it in the main site.
I was wondering if anyone knows why and what the best way round this is?

In addtion to DavidG's answer.
When you publish the project the compiled build does not have a Properties folder. To solve the issue while developing locally you can set RouteExistingFiles to true so ASP.NET routing handles all requests.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

The issue is that your project already contains a folder called Properties which is mainly used for the AssemblyInfo.cs file but has other stuff in there too. The engine used to resolve what files to send to the client prioritises files and folders over routing. so the URL http://site/properties is trying to server content from there, which is ultimately blocked anyway.

Related

Running Angular 2/4 Route instead of .net Core route

in my Angular Module I have the following Route:
{ path: "something/:id", component: SomeComponent }
which runs fine when using "routerLinks" frontend, but as soon as I manually write the ID in the URL and press Enter, It runs the C# backend method instead and ignores the angular route.
[HttpGet("{myid:guid}")]
The backend method returns Json as it should but I cant get my Angular route to display my component, seeing I go straight to the Controller.
So my question is: How do I tell my application to run my frontend route instead of my backend route, or work my way around this problem when manually writing the id in the url?
Any help would be greatly appreciated.
I realise this isnt strictly answering your question, but unless you have a specific requirement to handle them on similar routes, isnt it simpler to serve your API on a clearly distinct route using a subdomain or specific base url fragment?
E.g. front end routes go on:
www.example.com
API routes go on:
www.api.example.com or www.example.com/api/
Note that if you cant use the subdomain option, you'll need to configure URL rewrites in your web.config to route between your API and front end.
Have you tried configuring your startup.cs? On the configure method try:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});

Host WebApi and C# site under the same domain

I have a website made in C# (http://my.site/), and now i have a Web API project that i want to merge to that domain and listen on http://my.site/api. They are 2 separate projects in VS2013.
I tried uploading Web API to /api/ subfolder in the server and map a MVC route to it (WebApi route in WebApiConfig.cs still routeTemplate: "api/{controller}/{id}"), expecting the default (Home) MVC controller to respond in /api/. The MVC route (in WebAPI's MVC routing):
routes.MapRoute(
name: "Default",
url: "{controller}", //Also api/{controller}
defaults: new { controller = "Home", action = "Get", id = UrlParameter.Optional }
);
But i get "Server Error 404" when accessing http://my.site/api/ / http://my.site/api/home etc. I don't understand the nature of this error (most likely settings in the C# website in root folder), but one would think IIS would serve the default app inside the /api/ folder.
So i want to know if adding WebApiConfig.Register(GlobalConfiguration.Configuration); to Global.asax (Application_Start()) and adding WebApiConfig.cs to App_Start folder of the C# project (and using the respective namespaces) will solve the issue. This seem too simple to be true so i doubt it will work, so i need someone to point me in the correct direction, or i will commit the sin of going back to asmx.
If not, how can i have C# website and a WebAPI on same domain?
You are wanting to run two separate dlls (each with their own routing rules) within the same bin folder on your server. That could definitely cause some conflicts in routing on your site. A proper way to do this would be to host your api project on a separate server and use a sub-domain, such as http://api.my.site/. That way you ensure that your C# site (http://my.site/)would have access to the api (http://api.my.site/). And your dlls, including routing rules, would remain separate.

MVC RouteConfig

I've set up an MVC project, it works perfectly. But I upgraded with a new controller, added some new views, nothing special... The web page works almost perfect when online except that it can't find my new views online.
I really don't know why, because it works in all debug modes in localhost on my machine, but not when published, I did publish the entire solution.
I've also noticed that I can't add new pages to my working home-controller either, I can only edit the ones that I have.
I checked RouteConfig and there are only the default basic settings
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I've created a new controller called Questionnaires, do I have to add this to RouteConfig to make it run online?
Browser address:
www.<address>.com/home/OneView
But with the new Views I want this for the new pages:
www.<address>.com/Questionnaires/Mixer
When browsing to the address I receive this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Questionnaires/Mixers
You said it can't find your views, but routing isn't concerned with views. It's concerned with mapping requests to a controller and action method. The action method is what returns the view. If you created a new controller called QuestionnairesController, then you should be able to access it at /Questionnaires. And since that URL doesn't specify an action method, it will by default (because of your configured route defaults) look for an action method named Index.
If you want to access the Mixer action method in the QuestionnairesControllers at /Questionnaires/Mixer, that should work fine based on the routing you showed us.
When you do return View(); in your Mixer action method, it will by default look for a view at ~/Views/Questionnaires/Mixer.csthml (among several other locations, but that's where you most commonly put it).
This is a bit embarrassing, but I did not notice that i published my project in Debug mode, set to Release solved the problems. Never missed this before.
Sorry about all this, have mercy...

What is the difference between the way routes are registered with MVC and Web API?

In my MVC and WebAPI application I see two different ways of doing routes.
One for MVC which calls RegisterRoutes and passes RouteTable.Routes
One for Web API which calls CustomizeConfig and passes GlobalConfiguration.Configuration.
For WebAPI:
WebApiConfig.CustomizeConfig(GlobalConfiguration.Configuration);
public static void Register(System.Web.Http.HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: ApiControllerOnly,
routeTemplate: "api/{controller}");
}
For MVC:
RouteConfig.RegisterRoutes(RouteTable.Routes);
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.MapRoute("DefaultRedirect",
"",
new { controller = "Home", action = "Index" });
}
Can someone explain if there is any difference in me registering my routes in one or the other method calls? Also why is it done this way with one using.
MVC routes register with ASP.NET (system.web) route collection. Web API however is designed to run either in IIS on top of system.web or as a self host without changing the code.
Hence Web API has a different registration mechanism, where it can use the system.web routing under the hood, or it's own routing system when using self hosting (Either WCF self host, or Owin host are supported out of the box).
There is one other small difference, Web API routes require naming of the route, where MVC routes do not.
One of the significant differences in the web API compared to traditional ASP.NET MVC controllers is how the web API will route request into the action methods.
With the web API, the HTTP method being used plays a role. The HTTP method is the verb used in the HTTP message and the common verbs are get, post, put, and delete. Those are the verbs that the web API will route by default. You an also handle additional verbs if you need to do something like webDAV. You can do that and handle additional verbs by using an except verbs attribute.
What the web API will do is if there is a request for "/movies", the web API will look for a movie controller and then look for a method on that controller starting with the word "Get." So I could have an action called get movies and because this is an HTTP get message, the framework will invoke get movies. But you could also call it GET whatever is just because it starts with the letters G-E-T that's why that particular action method will receive the request.
The web API also registers these routes slightly differently since there is no action that's going to be in the URL for the routing engine to pick apart, it's using the verb instead. If you look at the default route configuration for a web API, it's done with an extension method MapHttpRoute.
For Web API it is:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
For MVC, it is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Notice that for Web API there is no action in the URL template and it's also important to note that the path of this needs to start with "api".
After looking at this, the proper way to reach a controller (say Movies) is to use /api/movies as a URL on the browser. That will invoke the get method since we have a get request.
Note: Web API controllers inherit from System.Web.Http.Controller, but MVC controllers inherit from System.Web.Mvc.Controller. Both the libraries are different but acts in similar fashion. Web API's MapHttpRoute is defined as an extension method in System.Web.Http class. MVC's MapRoute is defined in System.Web.Mvc as an extension method.

Help with mvc deploy on asp site

i have this problem... i have an ASP.NET MVC application locally and i have in one server another application in asp, i need to add the mvc application to the asp application. So i have "http://www.aspApp.com/mvcApp" but i cant work with the mvc application, when i call for example /Controller.aspx/Action, it throws a 404 error, and the address that i see in firebug for the get is "http://www.aspApp.com/Controller.aspx/Action", but this should be "http://www.aspApp.com/mvcApp/Controller.aspx/Action" i think... i try to change the post and get's in the mvc application to /mvcApp/Controller.aspx/Action instead /Controller.aspx/Action but it doesn't work at all, if in the explorer i put "http://www.aspApp.com/mvcApp/Controller.aspx/Action" it throws the 404 to. The IIS is 6.0 and i think i have the correct configuration for mvc.
I hope you can helpme, thanks!
Setup your routes like this,
// Classic URL mapping for IIS 6.0
routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new { action = "Index", id = "" }
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
You need to put the web.config inside your "mvcApp" virtual directory - so the mvc app will know the application root is at that directory instead of at the website root.
I fix it! The problem was on the iis configuration, the check "Verify that file exist" was checked and i uncheck and fix the problem.

Categories

Resources