Web Api Help Page route - asp.net-web-api

I was recently tasked with fixing one of our Help Pages that had gone down. I hadn't ever worked on one before, so I jumped in and started playing around with it. I noticed we had this route set up for the Help Page:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"HelpPage_Default",
"api/v1/Help/{action}/{apiId}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
HelpPageConfig.Register(GlobalConfiguration.Configuration);
}
I compared it to another working Help Page route and found the url to be different. I changed the url to
"Help/{action}/{apiId}"
and it worked. I've done some research online (this helped) but still fail to understand why changing the url would make any difference on whether that page would be hit. It would make sense to me that if I went to mydomain.com/api/v1/Help that I would still hit the help page with the original url.
Thank you in advance.

Route what you have "api/v1/Help/{action}/{apiId}" this is wrong because format of the route should be [Controller]/[Action]/[Id] and your controller is Help not "api/v1" .
And to answer your question to "mydomain.com/api/v1/Help" this url hitting the help page, yes it will if you give "help/{action}/apidid" url in the route.
"api/v1" till here its your IIS virtual directory setup not a route in your application configuration.

Related

MVC routing not working on godaddy but on azure subscription

I have an mvc site deployed at godaddy server. The mvc routing is working fine on my system and i tried to deploy it on my free azure subscription to test. Its working there. My route.config file has nothing new:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
On server, i have a virtual directory named "httpdocs". None of the controller is receiving my call and giving error 404 page not found. Can you please assist as to wat other changes do i need to check as it seems related to deployment on godaddy server. Please feel free to let me know if any other information seems missing here so that i can provide additional details.
Ok, After spending almost 2-3 days on it, i finally figured it out. My application is using MVC 5.2 Version and goDaddy provides support for max MVC 5 version.
So after downgrading MVC FROM 5.2 to 5 version, it worked.
This may help someone else who might face this issue.

Customize Authorize Redirect

I am using Beta 4 and when I use the [Authorize] attribute, it redirects to /account/login like i'd expect but that's no the name of my URL. I could customize this in web.config but I don't know where to configure it in ASP.NET 5. Any ideas?
Not sure if this will help but I did download VS 2015 RC.
I’ve created a new MVC 6 website project and launched (F5) it. While being unauthenticated, I tried reaching the ManageController which is decorated with the [Authorize] attribute.
Needless to say, I was redirected to the Account/login view but nowhere have I found where this is configured.
I did manage to add the following inside the ConfigureServices() method of the Startup.cs:
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/Gazou/Index");
});
Just above the:
services.AddMvc();
I’ve then created my new GazouController with a simple Index IActionResult().
Ran the application again, tried accessing the ManageController but this time, I was redirected to the Index method of my GazouController instead of the default behavior.
Hope this helps.
Vince
I think you didn't format it with code so I can't see it, but this is what I found:
services.Configure<CookieAuthenticationOptions>(opt =>
{
opt.LoginPath = PathString.FromUriComponent("/Auth/Login");
});

Laravel 5 Route Strange Behaviour

I have a Laravel site set up on a Homestead box, so I'm accessing it on sitename.app:8000. I have a route called "news" but when I try to go to sitename.app:8000/news I get oddly bounced out to sitename.app/news/.
If I change the routename to "news2" I can access the desired controller action as per normal at sitename.app:8000/news2. So somehow it's "news" itself that has become uncooperative - and I'm pretty sure that we aren't even getting as far as the NewsController, when I try to access that url.
Can anyone work out from these symptoms what might be going wrong? One "news"-related change I made at some point was to add $router->model('news', "App\News"); in the boot method of the RouteServiceProvider, but removing this doesn't seem to make the difference.
ETA: People keep asking for the routes.php file. I can literally remove everything from the file except
Route::get('news', function() {
return "hello world";
});
Route::get('news2', function() {
return "hello world";
});
and /news2 will work but /news will bounce me out. So I remain pretty convinced that the problem is somewhere deeper than routes.php...
I finally worked out what boneheaded action of mine had been causing this behaviour!
I had created a folder in /public named "news"... i.e. with the same name as an important route. Not sure exactly what havoc this was wreaking behind the scenes for Laravel every time a request for /news was being made, but one can assume it was nothing good.
Advice for anyone tearing their hair out over a route that "mysteriously doesn't work" - check your public folder for possible collisions!
This is a known issue Larvel missing port
The easiest way to solve this problem is to go to public/index.php and set the SERVER_PORT value.
$_SERVER['SERVER_PORT'] = 8000;
Don't forget to set the base url in the config if you are using links on website, the url generator uses the base-url defined in the config.
Last option is to change the vm portfoward in VagrantFile to point to port 80 and use port 80 for your app.

ASP.NET MVC 3 Access Denied in just one server and one folder

I'm with the following issue: when I try to access a certain page (http://mysite.com/Client/) I get an Access Denied error (403 - Forbidden: Access is denied), but if I try to access the same URL using "Index" in the end (http://mysite.com/Client/Index), it works! And I have a lot of another folders that work without the "Index" in the URL.
The wierdest part is that in the test server (http://mysite.com:8080) I can access in both ways (/Client/ and /Client/Index/). By this time I don't know if it's an IIS 7 problem, or in the Client Folder for some reason, if it's in Web.config or if I'm just losing my mind!
The routes are the following:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
One more thing: the test server app and the main server app are the same!
Can someone give me a hand? Thank you!
P.S.: There isn't any authorization setting in web.config.
From your question it seems like you don't have a Client controller, just folder. So, I guess the route is looking for a controller that It can't find. Or you have a controller but no actions in it. I am not sure, anyway, try adding this to your routes:
routes.IgnoreRoute("Client/{*path]");
EDIT:
Try adding this route:
routes.MapRoute(
"Default", // Route name
"{controller}/Index", // URL with parameters
new { controller = "Home", action = "Index"}
);
resolved in a mysterious way: although I tried to resolve things editing the routes, I realized that the error was occurring before the application_start event. So, the problem was in IIS.
I tried to find the reason for the problem, but I couldn't, so I installed the URL Rewrite 2.0 and made the request of "/Client" redirect to "/Client/Index".
Wierd, isn't it? But this workaround resolved!
Thaks for all the answers!

StructureMap controller factory and null controller instance in MVC

I'm still trying to figure things out with StructureMap and one of the issues i'm running into is my Controller Factory class blowing up when a null controller type is passed to it. This only happens when the application builds for the first time, after which every subsequent build works fine. Even when i shutdown Visual Studio and reopen the project (I'm not running this in IIS). It's almost like there is some sort of caching going on. This is what the controller class looks like:
public class IocControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
try
{
return (Controller)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
}
What could be wrong? Do i need to have every controller registered? Thank you.
Most browser are looking for a favicon.ico when you load a site, and there is probably some caching involved with this behavior, this might explain the odd "Only fail on the first build" thing you mentionned.
In my case this was causing the problem of the null controller type in the controller factory.
Adding a routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" }); in global.asax makes the error go away, the request should fall through to the filesystem without MVC looking for a favico.ico controller in your code.
Here is a link to Gunnar Peipman post about this
I found out by overriding GetControllerType(string controllerName) in my custom controller factory class and checking what the controllerName value was for each request.
I ran into the same problem with a controller factory built around ninject.
It seems MVC will pass you null for controllertype when it can't resolve a route from the routing table or when a route specifies a none existing controller. I did two things to solve this. You might want to check your route table and add a catchall route that shows a 404 error page like described here .Net MVC Routing Catchall not working
You could also check with the routing debugger what goes wrong.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
I was having the similar problem. I believe it was HTTP requests for nonexistent images, CSS files, etc.
We know the MVC routing first looks to see if the requested file physically exists. If it doesn't then the URL gets tested against the configured Routes. I think the request for an image that didn't physically exist was passed to the Routing engine, and didn't match any routes, so NULL was used.
So to fix it, use FireBug or something to watch for, and fix, broken HTTP requests. During development, I used a route like this to temporarily bypass these issues (all of my resource folders start with an underscore like _Images, _Styles, etc):
routes.IgnoreRoute("_*"); // TODO: Remove before launch
Hope this helps!
What I think you need to do is exactly the same thing that the default MVC controller factory does on the GetControllerInstance method. If you look at the Microsoft source code for DefaultControllerFactory at http://aspnetwebstack.codeplex.com/ you will see that the DefaultControllerFactory throws a 404 Exception when controllerType is null. Here is how we do it based on this information:
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return base.GetControllerInstance(requestContext, controllerType);
var controller = ObjectFactory.GetInstance(controllerType);
return (IController)controller;
}
}
Basically this will ensure that, when user enters an invalid route the application handles it as a 404 error.

Resources