asp.net mvc3 to mvc4 conversion exception in only some pages - asp.net

I am changing an MVC3 app to MVC4(all the proper changes have been done in web.config). The application is loading fine as are some pages but some pages which were working previously in MVC3 are throwing the same kind of error as below: I have done the references correctly and no code was changed during the conversion process
Can anyone tell me the solution for this error
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'
This happens when I call this method in a cshtml like this
#{ Html.RenderAction("GetTestItem", "TestItemsInCart", Model.TestItemsList);}
and the GetTestItemmethod is
[HttpGet]
public ActionResult GetTestItem(TestItemsList test)
{
TestViewModel xyzViewModel = new TestViewModel ();
return PartialView("_TestXyz", xyzViewModel );
}
The method is getting called in the controller and it is returning the result to the view only in the cshtml I am getting the stated exception.
This was working previously in mvc3
Thanks

Here are the steps that you need to follow to manually upgrade an existing ASP.NET MVC 3 application to MVC 4: http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806
Also the error message that you are getting indicates that you have some missing closing braces in your Razor view. Make sure you have fixed it or show your Razor view if you expect other people be able to help you.

Check for opened brackets. Any opened bracket { needs to be closed with another bracket }.
Check for naming of the variables.

Related

AspNet.Idendity: determine whether the user is in a specified Role

I am programming an WebApp with MVC5 and I will check if a User is in a specified Role.
Therefore I have tried to use User.IsInRole("Role") but it throws an Exception.
I have tried with importing AspNet.Identity and using the User Manager but it can not find the method IsInRoleAsync(userId, role):
#using Microsoft.AspNet;
#if(UserManager.IsInRoleAsync(user.Id, "Role")){
// some code
}
Please notice that I use it in a cshtml file and razor syntax. I hope you can help me.
Thank you in advance
Hi you can easily do in view with the following code:
Ex: view1.cshtml
#if (Request.IsAuthenticated && User.IsInRole("Administrators"))
{
//Any code
}
The code UserManager.IsInRoleAsync does not just work inside a Razor view (cshtml). You would have to instantiate an ApplicationUserManager instance, typically called "UserManager" as well as have a valid ApplicationUser ("user") object.
You are better off to do this work in the controller and pass it to the view with a View Model or other means like ViewBag or ViewData.

Glimpse showing empty Execution/SQL tab when using structuremap container for asp.net mvc5

I am trying to integrate Glimpse into my asp.net mvc-5 application. I'm using structuremap to inject constructor dependencies in my Application_Start method.
_structureMapResolver = new StructureMapDependencyResolver(container);
DependencyResolver.SetResolver(_structureMapResolver);
I am getting empty Execution and SQL tabs. I tried commenting the above lines and loaded a regular empty constructor page, and I started getting results.
how do I get this data in this tab keeping structure map intact?
(let me know if you want the code for StructureMapDependencyResolver class.)
Thanks already.

Prism for Xamarin.Forms: I cannot resolve the navigationService unless if ViewModelLocator.AutowireViewModel="True"

Have been stuck for days ... and then I found what was the issue
I have two pages one in case of tablet and the other in case of phone . Since they have different names then using ViewModelLocator.AutowireViewModel to share the same view model as a binding context does not work . So for this case I am using the classic binding context, as i did not want to create a viewmodel copy for each page.
My problem was when I run it gives me an error saying "no parameterless constructor defined for this object prism" The reason of this error is that the viewModel has a contructor that expects a navigationService.
To solve this error i have to remove the navigation service as an argument , but then I need it so i can navigate with it .
In this case I would recommend using the ViewModelLocationProvider.Register method. So in the application's RegisterTypes method, you can put a condition similar to this:
if (Device.Idiom == TargetIdiom.Tablet)
ViewModelLocationProvider.Register("MainPage", () => your logic);
else
ViewModelLocationProvider.Register("MainPage", () => your logic);
EDIT: Actually, you'll still have to manually resolve the INavigationService doing it this way. I am going to reopen your Prism Issue. We can look into improving the ViewModelocationProvider.Register method to enable this scenario.

What's the right way to call an action from a different controller in ASP.Net MVC 6

I'm getting a
No route matches the supplied values
while trying to return a RedirectToAction("Action", "Controller"). The method signature says "actionName" and "ControllerName". I'm assuming actionName is the method name in the Controller, Am I correct? For ControllerName I'm using the Controller File Name without the Controller Sufix. Ex.:
return RedirectToAction("Index", "WebApp")
where Index is a method of WebAppController and the command is being issued from a method of AnotherController
Both the caller controller and the called one are on the same Controllers directory on the same application.
I'm cofused because in this ASP.net MVC application there is also Route attributes and Action attributes where you can put names on methods, different than the real method name. In my case I have no Route["Name"] nor [httpXXX("route", Name="dasdasdas")] configured for the methods involved in my attempt.
I have been reading MS docs and some examples but It appears I'm doing the thing right but for strange reasons it's not working. I even tried using Redirect("Controller/Action") and with it the problem vanishes but the new problem is this way of redirect doesn't support passing data parameters to the target route.
At this point I'm not working with Action links in Views, different from Form related ones.
I would really appreciate if at least anyone can give me a hint about where can I find info.
The right way to call an action from a different controller is the one I was using:
return RedirectToAction("AnActionMethodName", "AControllerWithoutControllerSufix"[, object values]);
My problem, after several hour spent was that I added two useMvc calls in the Startup.Configure(...)method:
app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=MyApp}/{action=Index}/{id?}");
});
This was due to copy + paste of code. It was obviously confusing the MVC component in charge of attending the redirection part, as the right way I supose is tho use only one. (I'm newbie on .Net Platform and its frameworks)
Anyway I leave this as a reminder about the risks and consequences of copying and pasting code. At some point something weird can happen to your app and the cause can be a simple copy + paste error.

ASP.NET MVC 2: Avoiding loop when accessing 404 action directly

Like many people using ASP.NET MVC, I've implemented my own custom 404 error handling scheme using an approach similar to the one described here: How can I properly handle 404 in ASP.NET MVC?
(I actually discovered that post after implementing my own solution, but what I came up with is virtually identical.)
However, I ran into one issue I'm not sure how to properly handle. Here's what my 404 action in my ErrorController class looks like:
public ActionResult NotFound(string url)
{
url = (url ?? "");
if (Request.Url.OriginalString.Contains(url) &&
Request.Url.OriginalString != url)
{
url = Request.Url.OriginalString;
}
url = new Uri(url).AbsolutePath;
// Check URL to prevent 'retry loop'
if (url != "/Error/NotFound")
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
// Log 404 error just in case something important really is
// missing from the web site...
Elmah.ErrorSignal.FromCurrentContext().Raise(
new HttpException(Response.StatusCode,
String.Format("Resource not found: {0}", url)));
}
return View();
}
The part that's different from the answer in the other StackOverflow question I referenced above is how the 'retry loop' is prevented. In other other answer, the code that prevents the retry loop simply sets properties on a ViewModel, which doesn't seem to actually prevent the loop. Since the action is sending back a response code of 404, then accessing the action directly (by typing "/Error/NotFound" in the browser) causes an infinite loop.
So here's my question: Did I miss another, more obvious way to handle the retry loop issue, or is my approach a decent way to do this?
you can handel erros by enabling the customErrors mode in the web.config file and set it to redirect errors to your controller when any errors occurs.
see an example here

Resources