I'm trying to get the open authentication for google working in my application. Using information found in the comments of this answer: https://stackoverflow.com/a/23094155/526704
So I added this route to the top of my RegisterRoutes method:
routes.MapRoute(
name: "signin-google",
url: "signin-google",
defaults: new { controller = "Account", action = "ExternalLoginCallback"}
);
Before doing this, navigating to localhost:port/signin-google gave me a 404, but now it just gives me an empty page. When I navigate directly to /Account/ExternalLoginCallback, it sees it wasn't given any login data, so it returns me to the login page (per the logic in the controller). When I put a breakpoint at the top of the ExternalLoginCallback method, it fires when I navigate to it directly, but not when I load /signin-google.
Why is the route returning an empty page?
There must be some sort of built-in route for /google-signin. I didn't solve this exact issue, but I was able to get Google OAUTH working with information from this link:
http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
Related
I have tried to check the browser in RouteConfig.cs file, but its showing error that didn't get the request from browser.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
string browser = HttpContext.Current.Request.Browser.Browser;
if (browser == "Chrome" || browser == "Firefox")
{
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
}
}
My goal is to access applications only from chrome and firefox, so I have tried to check it in RouteConfig to re-route to another view if condition not satisfied. But it's not working so I have checked with Global.asax but the routing is not working there.
You can't check for the browser at startup, since the startup isn't related to a request, so no browser to check there.
The best option you have is to check in the action or controller itself what the browser is. I would be wary though to exclude specific browsers form visiting your site, as it looks like you are trying now.
For browser-checking, a good option is to configure a middleware class that you can use to check each http request, and potentially specify a redirect or response. It is a suitable way to contain this logic all in one place, so you don't need to duplicate across controllers or actions, and is really easy to set up. Lots of articles are available online.
I'm facing an issue in a project that I'm currently working on, I'm using ASP.NET MVC.
The scenario as follow:
- I have a login page (Username and Password).
- Whenever I navigate to localhost:5588/login, the below action method will be called 2 or 3 times (I'm using a Break Point inside this method to catch the call).
public ActionResult Login()
{
return View();
}
The question is, why this method is called 2 - 3 times whenever I enter the login page ?
P.S #1: Not only the Login page is being called 2-3 times, also each Action method have the same issue.
P.S: #2: I'm using the below route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
UPDATE:
This issue happens only on Google Chrome!
Issue might happen, because browser can pre-load page, before you hitting Enter.
In this thread posted solution, how you can understand that it is pre-load request: HTTP header to detect a preload request by Google Chrome
I once had this issue and found I had something like this
src="#"
in my image tag or check for any other markup that could be accidentally referencing the page like Script references, image references, css reference etc
Obviously somethings call your action method 3 times, strongly advice to open chrome developer tools, navigate to network and check the traffic, if they are XHR requests you can also track from where they came from, otherwise it is shoot in the dark.
This is the correct answer and this actually solved the issue. [Special Thanks to my friend Mohammad Aldayem for the great finding].
Based on an issue posted on Chromium Issue Tracker (https://bugs.chromium.org), the issue was in the "favicon.ico" in the _Layout.cshtml page. Because Chrome requests favicons on every request on pages that don't have a favicon.
And here are the links for this issue while using Google Chrome:
Link #1: https://bugs.chromium.org/p/chromium/issues/detail?id=64810
Link #2: https://bugs.chromium.org/p/chromium/issues/detail?id=39402
I've created a controller, called ClientController.cs and VS automatically created the necessary View files in /Views/Client. But I wanted to get these pages in a different URL... So, it is /Client but I need it at /admin/client.
What should I change?
Thank you!
It's not clear what your functionality will be in the long run, but here are a few options that allow you to get the URL format you want:
Perhaps you want a controller called "Admin" and an action called "Client". This would give you a path of /Admin/Client by default
Alternatively, you can change your route maps. For example, the following with route /Admin/Client to the Index of your Client controller:
routes.MapRoute(
"Default", // Route name
"Admin/Client/{action}", // URL with parameters
new { controller = "Client", action = "Index" } // Parameter defaults
);
Or maybe even go a far as using "Areas", depending on what you need. Have a Google of that if you're interested in learning more
If you want it to be admin/client, then using the default routing you should create an Admin Controller with an ActionResult method called Client. Your views folder should have an admin folder with your client view inside.
I haven't done a lot of MVC but i believe this is what you do.
Pretty new to MVC I have a page on an open source application I have downloaded that is at the url...
http://localhost:51930/admin/login?databaseIssue=true
Obviously Im trying to find which controller and view this maps to in the application. How do I work this out? What should I search for and where to look?
Also how do I work out which actions process this view?
This should help you out. This tool is awesome!
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
This guide should get you started. Basically you work with a collection of routes and their arguments, in the global.asax.cs file. The guide there also has a section on custom routes.
By the defaulting routing rules, it's {controller}/{action}/
Which would make the controller in http://localhost:51930/admin/login?databaseIssue=true admin and the action Login.
By convention, MVC routes are generated in form
{app_base}/{controller}/{action}
Check out this stackoverflow question for more information.
So in your case, you'll want to look for an admin.cs class in your Controllers folder.
global.asax is where the route mapping is defined.
You'll see/set something like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
so by default, your example maps to admin = {controller} and login = {action} and login action method would take the databaseissue=true bit as a parameter.
All these answers are good, except in the case where someone may have created a custom route to the specific url in question. By default, they are all correct, but if a custom route was setup, it could be going to the StackController and referencing the Overflow action.
Like Jamie R Rytlweski suggested above, reference RouteDebugger in your project, add the hook in your global.asax and try going to that page, it will show you a listing of all the routes defined in your application and then show you which routes the current page matches
I have the following in my Global.asax.cs
routes.MapRoute(
"Arrival",
"{partnerID}",
new { controller = "Search", action = "Index", partnerID="1000" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
My SearchController looks like this
public class SearchController : Controller
{
// Display search results
public ActionResult Index(int partnerID)
{
ViewData["partnerID"] = partnerID;
return View();
}
}
and Index.aspx simply shows ViewData["partnerID"] at the moment.
I have a virtual directory set up in IIS on Windows XP called Test.
If I point my browser at http://localhost/Test/ then I get 1000 displayed as expected. However, if I try http://localhost/Test/1000 I get a page not found error. Any ideas?
Are there any special considerations for running MVC in a virtual directory?
IIS 5.1 interprets your url such that its looking for a folder named 1000 under the folder named Test. Why is that so?
This happens because IIS 6 only
invokes ASP.NET when it sees a
“filename extension” in the URL that’s
mapped to aspnet_isapi.dll (which is a
C/C++ ISAPI filter responsible for
invoking ASP.NET). Since routing is a
.NET IHttpModule called
UrlRoutingModule, it doesn’t get
invoked unless ASP.NET itself gets
invoked, which only happens when
aspnet_isapi.dll gets invoked, which
only happens when there’s a .aspx in
the URL. So, no .aspx, no
UrlRoutingModule, hence the 404.
Easiest solution is:
If you don’t mind having .aspx in your
URLs, just go through your routing
config, adding .aspx before a
forward-slash in each pattern. For
example, use
{controller}.aspx/{action}/{id} or
myapp.aspx/{controller}/{action}/{id}.
Don’t put .aspx inside the
curly-bracket parameter names, or into
the ‘default’ values, because it isn’t
really part of the controller name -
it’s just in the URL to satisfy IIS.
Source: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
If you are doing this on Windows XP, then you're using IIS 5.1. You need to get ASP.Net to handle your request. You need to either add an extension to your routes ({controller}.mvc/{action}/{id}) and map that extension to ASP.Net or map all requests to ASP.Net. The http://localhost/Test works because it goes to Default.aspx which is handled specially in MVC projects.
Additionally, you need to specify http://localhost/Test/Search/Index/1000. The controller and action pieces are not optional if you want to specify an ID.
There are a number of considerations when using virtual directories in your application.
One is particular is that most browsers will not submit cookies that came from one virtual directory to another, even if the apps reside on the same server.
Try set virtual path: right click on mvc project, properties, web tab, there enter appropriate location.