Construction #User.IsInRole("admin") return only false - asp.net-core

I have got a short question: recently I have created a web application in ASP.NET Core 3.1, where I have been using Identity framework to authorize/autenthize of an user.
In navigation part of page I would like to display a role of user, so I wrote following code snippet in partial view:
#if (User.IsInRole("admin") {...}
But there is a problem, mentioned construction permanently returns "false".
Any idea to fix this error?
Thank you very much.

Related

Porting Yii1's $user->setState('key', 'value') to Yii2

I'm upgrading a Yii1 app to Yii2 and got stuck at porting
Yii::app()->user->setState('key', 'value');
Yii::app()->user->getState('key');
to
Yii::$app->user->XXXX('key', 'value');
Yii::$app->user->XXXX('key');
I know the setState method sets a value in the session (And db if the session is also in the db), but I can't figure out which of the new \yii\web\User method I should use as none of the login, setIdentity, switchIdenty methods seem to work the same way.
More info if needed: The "setState()" code is used to save the user id entered in a text field, which will then be used on the next page to ask the password.
This feature was dropped in Yii2. You should use Yii::$app->session in this cases, because setState/getState was just simple wrappers on it.
About prefix for user session
Simply use
Yii::$app->session->set('user.some_param',$someparam);
and use below to retrieve
Yii::$app->session->get('user.some_param' , $defaultValue);
Or you can implement this feature from Yii1 in your own class. For flashes see CODE

Yii2 "Page not found" when using CRUD generator

I have succesfully used Yii2 Model & CRUD Generators to obtain some skeleton code files for my web app. Particularly, the CRUD Generator claims to have succesfully created its view files into:
<yii_root>/basic/views/<my_view_name>/*.php
which I got by leaving "View Path" field blank.
However, browsing to:
https://<my_site_FQDN>/basic/web/index.php?r=<my_view_name>/index
spits a "Not Found (#404)" error and I'm unable to find any useful info in the Yii2 debug logs.
Any idea on this matter shall be welcome.
Antonio
<my_view_name> this is a terrible way of looking at this. Read about MVC.
You are creating controllers, routes are to controllers not to views. Stop looking if the views are there... look if the controller is there. You never interact with a view, you always do with a controller. So, is your controller there? are you sure you have created it?
Also what is the controller name? if you have something like ProductCategory then the correct route is
https://<my_site_FQDN>/basic/web/index.php?r=product-category/index
and not
https://<my_site_FQDN>/basic/web/index.php?r=ProductCategory/index
Edit
Ok, I see your problem, stop putting folders under other folders and so on. You created your CRUD wrong. Your controller has to be directly under controllers not under controllers/bibliografia, the same goes for the model. Delete the files and start again with CRUD as probably your namespaces are also wrong.
In my case, Yii2 consistenly writes the following with gii CRUD auto-generator.
namespace app\Controllers;
Notice uppercase 'C' for Controllers. This causes 404 error.
It should be :
namespace app\controllers
This fixed my 404 error.
I found with Yii2 (advanced template) that the generated controllers had:
namespace app\controllers;
I had to change this to:
namespace frontend\controllers;
I fixed it by capitalizing the first letter of the controller name.
Controller name should start with capital letters.
If lower case is used, Controller generator won't throw any error, but you will get not found(404) error in the browser.
this threw an error:
app\controllers\testController
this worked:
app\controllers\TestController
Its very Simple, change the namespace and please be careful while creating CRUD..
change namespace app\controllers; to namespace backend\controllers;

MVC 4 Areas not working properly

I have created a sample VB project in VS2010 to add Areas to the web UI but when I run it gives the following error:
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.
I also added AreaRegistration.RegisterAllAreas() to the global.asax file and also tried to manually register the route in the route.config but nothing is working.
routes.MapRoute( _
"Admin_default", _
"Admin/{controller}/{action}/{id}", _
New With {.action = "Index", .id = UrlParameter.Optional} _
)
It looks like it only finds the root views but not the Area specific view. Any ideas??
Found an answer on another site so posting the solution here:
The same project in C# works perfectly fine but fails in VB.
The reason: The controllers namespace is wrong in VB.net
Solution: Change the namespace of the controller in the vb project to MyApplication.Areas.MyArea.Controllers and then run it, will be fine.
Make sure you add AreaRegistration.RegisterAllAreas() as first line in Global.asax. Because areas should be registered before registering other routes.
Also in the route you mentioned, there is no default value for controller. So, make sure you provide controller value in url or provide default controller parameter in route.

Yii with Bootstrap error (CWebApplication.bootstrap not defined)

I have followed this setup, but not work for me http://www.cniska.net/yii-bootstrap/setup.html, all CSS files do not give style to the page,
So searching I found this thread How to install bootstrap extension in yii using some tricks. But not work's too.
Now I have this error
'Property "CWebApplication.bootstrap" is not defined.'
Anyone can help or have any idea?
Thanks in advance and for my English.
Property "CWebApplication.bootstrap" is not defined.
The error means the system is trying to access Yii::app()->bootstrap, which could be a property on the application. Since bootstrap is a component it should be accessible, if the configuration is done properly.
This error is most likely because you forgot the following in the main config file:
'components'=>array(
'bootstrap'=>array(
'class'=>'bootstrap.components.Bootstrap',
),
),
// This should point to the location you placed the files in for example extensions/bootstrap/components/bootstrap.php
The class location above will only work if the alias is set properly before setting the configuration.
Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap');
Instead you could also change "bootstrap.components.Bootstrap" into "application.extensions.bootstrap.components.bootstrap".
I strongly advise you to use Yiistrap, it was made by the person who create yii boostrap and the person who created Yiibooster. Yiistrap has everything from both extensions and more new stuff. here is the page http://www.getyiistrap.com , also has a fully documented API which yii boostrap do not have and it will save a lot of google searches.

MVC3 Razor View Project using VB.NET gives Syntax error in Error List window for all vbhtml pages

Using MVC3 and Razor View engine, I created a VB.NET web application in VS 2010. This creates the default Account and Home Controller along with corresponding Action Views.
Now if I open any vbhtml file I get the following error message in the Error List window.
Error 50 Syntax error. C:****\MVC3AppVB\Views\Account\LogOn.vbhtml MVC3AppVB
(See screenshot here http://www.flickr.com/photos/7672540#N07/5469248676/)
Who ever this app compiles and runs without any problem. I tried to create the same project using C# and there is no error message in the Error List Window.
Can anyone explain why this error message shows up only for VB and not for C#?
Thanks in advance.
The problem is with MVC3 and webpage 1.0 tooling. This issue has been answered in codeplex.
Probably some bug with the Intellisense. Try recompiling the project. Unfortunately the Razor Intellisense is far from perfect. I hope it will be improved in future releases of Razor.