I'm trying to create a http://domain.com/NotAuthorized page.
went to Views\Shared and added a View called NotAuthorized witch originates the file name NotAuthorized.aspx
in my Routes I wrote
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized.aspx" // Route Url
);
but every time I access http://domain.com/NotAuthorized I get an error
The resource cannot be found.
What am I missing?
How can access this without using View("NotAuthorized") in the Controller, in other words, not passing through any controller.
You can't access views directly without passing through a controller. All pages in the Views folder cannot be served directly. So one way to accomplish what you are looking for is to write a custom[Authorize] attribute and set the error page:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
}
}
I still have no idea on how to accomplish it, but what I did was use the Home Controller and create an Action called NotAuthorized
public ActionResult NotAuthorized()
{
return View();
}
And add a route like
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized", // URL with parameters
new { controller = "Home", action = "NotAuthorized" } // Parameter defaults
);
And works fine now, I can easily redirect in any part of my Business Logic to /Notauthorized and that will route fine.
Related
I'm trying to implement the basic Passport integration in SailsJS. In my policies.js file, I have the default settings that every tutorial mentions.
'*': ['passport', 'sessionAuth'],
'auth': {
'*': ['passport']
}
My issue is that going to the main page localhost:1337/ doesn't seem to get passed through either policy. If I just set false there, everything still works. If I set false on the auth object for '*' though, I will get Forbidden on any /auth/* route. So, the policies seem to work, I just don't understand why the default catch-all doesn't. Thanks.
Do you use a controller or do you directly serve a view like in the sample homepage?
If you are serving the view directly with something similar to this:
// in config/routes.js
module.exports.routes = {
'/': {
view: 'homepage'
}
}
then you will have to modify it and use a controller in order to te able to use policies.
Create a route to a controller instead of a view:
// in config/routes.js
module.exports.routes = {
// Delete the previous definition and declare a route
// to a controller "index"
'get /': 'indexController.home'
}
Create the controller:
// in api/controllers/IndexController.js
module.exports = {
home: function (req, res) {
// Render the view located in "views/homepage.ejs"
res.view('homepage');
}
};
Then you will be able to manage the policies to apply to the controller index in the file config/policies.js.
I'm working on to write a new payment plugin on nopcommerce.
How can I render a new partialview in PaymentInfo.cshtml? The new view locates the same path with PaymentInfo.cshtml.
to render any view in the plugins for nopCommerce it must be embedded resource and it must be written in fully qualified name like
#Html.Partial("Nop.Plugin.Something.Something.Views.Plugin.VIEWNAME", item)
You can create a Custom View Engine and add that to your payment plugin. This so you can add routes to Nopcommerce.
First you create a Custom View Engine
CustomViewEngine.cs
public class CustomViewEngine : RazorViewEngine
{
public CustomViewEngine()
{
PartialViewLocationFormats = new[] { "~/Plugins/Misc.HelloWorld/Views/{0}.cshtml" };
ViewLocationFormats = new[] { "~/Plugins/Misc.HelloWorld/Views/{0}.cshtml" };
}
}
Change paths of the Misc.Helloworld to the path of your plugin.
Change the .Edit to the action in your controller.
Change the other information of the controller and the action to the names in your plugin.
After that, create a RouteProvider.cs
ViewEngines.Engines.Insert(0, new CustomViewEngine());
var route = routes.MapRoute("Plugin.Misc.HelloWorld.Edit",
new { controller = "HelloWorld", action = "Edit", },
new { },
new[] { "Nop.Plugin.Misc.HelloWorld.Controllers" }
);
routes.Remove(route);
routes.Insert(0, route);
Change Misc.HelloWorld to the path of your Payment.Plugin,
After that u can add the following to the embedded source
#Html.Partial("Actionname", item)
For more information, see the blog of Alex Wolf
I'm trying to use the routes.js to define a route to '/account'.
I want whoever is trying to access that path to go through the UserController and the checkLogin action and if the security check passes, then the user should be rendered with the defined view which is home/account
Here is my code:
routes.js:
'/account': {
controller: 'UserController',
action: 'checkLogin',
view: 'home/account'
}
policies.js:
UserController: {
'*': 'isAuthenticated',
'login': true,
'checkLogin': true
}
It let's me view /account without going through the isAuthenticated policy check for some reason.
There looks to be a little confusion here as to how policies, controllers and views work. As #bredikhin notes above, your controller will never be called because the route is being bound to a view. It's also important to note that policies cannot be bound to views, only to controllers. The correct setup should be something like:
In config/routes.js:
'/account': 'UserController.account'
In config/policies.js:
UserController: {
'*': 'isAuthenticated' // will run on all UserController actions
// or
'account': 'isAuthenticated' // will run just on account action
}
In api/policies/isAuthenticated.js:
module.exports = function(req, res, next) {
// Your auth code here, returning next() if auth passes, otherwise
// res.forbidden(), or throw error, or redirect, etc.
}
In api/controllers/UserController.js:
module.exports = {
account: function(req, res) {
res.view('home/account');
}
}
To put it short: either controller/action-style or view-style routing should be used within the same route in routes.js, not both simultaneously.
According to the router's source code, once there is a view property in a route object, binding stops, so basically Sails never knows to which controller your /account path should be routed, which means that your UserController-specific policy config never fires.
So, just remove the view property from the route, you can always specify the view path (if you want a non-standard one) with explicit rendering from within your action.
For statics work with policies, you can set your route with controller and action:
'GET /login': 'AuthController.index',
And set view/layout in your controller:
index: function (req, res) {
res.view('auth/login', { layout: 'path/layout' } );
},
public ActionResult Home()
{
return View();
}
This is what I have for my current site within the HomeController. How can I detect if the action is called from a mobile device, and if it is, re-direct to MobileHome.aspx instead of Home.aspx.
I don't need to know the details of the device, because I am implementing the view in JQuery Mobile, which should adjust itself correctly depending on the view it's rendered in.
You may find the following blog post useful.
The following is an override on the Controller class. I have not tested this, so consider it pseudo code:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (this.Request.Browser.IsMobileDevice && filterContext.Result is ViewResultBase)
{
var viewResult = filterContext.Result as ViewResultBase;
viewResult.ViewName = "Mobile" + viewResult.ViewName;
}
base.OnActionExecuted(filterContext);
}
You can use the Request.Browser.IsMobileDevice to determine if the device is mobile (obviously), and then check to see if the result it a view. However, changing the view name is not sufficient if you pass an actual view to the result of your action.
For some reason my application isn't routing to my controller method correctly. I have a routelink like this in my webpage -
<%= Html.RouteLink("View", "Blog", new { id=(item.BlogId), slug=(item.Slug) }) %>
In global.asax.cs I have the following routes -
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MoreBlogs",
"Blog/Page/{page}",
new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
"Blog",
"Blog/View/{id}/{slug}",
new { controller = "Blog", action = "View"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And then I have a class BlogController that has a method -
public ActionResult View(int id, string slug)
{
... etc.
}
I put a breakpoint in the first line of the View method but it's not getting hit at all. I checked with a route debugger for the format localhost/Blog/View/1/test and it matched my custom route. All I'm getting is a 404 while running this, I can't work out why the route won't post to the view method in my controller - any ideas?
(Assuming the route debugger was based on Phil Haack's debugger post)
If you're getting a 404, that makes me think the actual view page itself cannot be found.
Assuming that you are also using areas within the application, and that RouteLink is actually being called from within an Area, I think you may need to specify area="" in your routeValues object ( you also need to specify the controller). In general, I think that you will need to add the area="..." part with all your Routelink calls when using Areas.
This is something that I have picked up about RouteLink and Area, but cant seem to find any reference material detailing the limitations.