Cannot call Action method from UrlHelper on ASP.NET MVC 5.1 - asp.net-mvc-5

I am trying to create unit tests for my ASP.NET MVC 5.1 app.
I want to verify the outgoing URL by using the UrlHelper.Action() method.
I can do that with ASP.NET MVC 4, but cannot with ASP.NET MVC 5.1.
There is an exception when call the Action() method:
An unhandled exception of type 'System.NotImplementedException' occurred in System.Web.dll
Additional information: The method or operation is not implemented.
Please help me a solution!
Thanks

I ran into the same issue. In my case, the NotImplementedException was coming from HttpContextBase.GetService.
The following code solved the problem:
public override object GetService(Type serviceType)
{
return DependencyResolver.Current.GetService(serviceType);
}

Related

Purpose of Error method in asp.net Core 2.0 MVC project template

I'm new to asp.net Core 2.0.
When you Create a new ASP.net Core 2.0 MVC Project (Web Application (Model-View-Controller)), you get a HomeController with Index(), About() and Contact() methods as in MVC 5.
However you also get this:
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
I can't find any documentation in Google or in any books as to what this is for and what the purpose of the single line of code is.
Can somebody please explain it? Is it some kind of best practice - should we be using this?
Thanks.
If time permit, I'll try to update my answer. But for now, the brief explanation is that every public method in your controller is callable as an http endpoint. For instance, the index method in your HomeController renders your index.cshtml view, the About renders the about.cshtml view and so on. In particular the public IActionResult Error() method is called if you redirect the user to the action when an error occurred. Consequently, it returns an Error View containing the information about the error, such as the RequestId and other values bonded with the ErrorViewModel.
I hope this help for now.

Unable to use customer class asp.net core post method [FromBody] attribute

In my asp.net core web api project, when I try to an entity/model using post method, its failing.
[HttpPost]
public IActionResult AddDeviceController([FromBody] DeviceController controller)
{
if (controller == null) return GetActionResult(HttpStatusCode.BadRequest, "DeviceController cannot be null");
return GetActionResult(service.AddDeviceController(null, null, null), HttpMethod.Post);
}
Here DeviceController model exists in a seperate assembly.
If I copy this model code to the web api service, it works perfectly! However, it fails to load when I refer it from the Models project. In this case, I receive System.IO.FileNotFoundExceptionexception with an error message Cannot load assembly C:\\3_SourceCode\mywebPI\bin\Debug\netcoreapp2.0\MywebApi.Models.dll
Please help me to get over this.
Check for the dll in the dependencies and in bin.

Running an ASP.NET Core Pipeline using Azure Functions

This was posted back in August https://social.msdn.microsoft.com/Forums/expression/en-US/c3e9f882-541f-48a2-a856-956ad3383f67/running-an-aspnet-core-pipeline-using-azure-functions
He talks about using a catch-all function and then somehow running the HttpRequestMessage through Asp.Net Core
How can I do this? With functions 2, using core, I can do things like add a Razor Page to my functions app. But if I try to return the View it throws an internal server error.
How can I call a Controller myself, and then get an IActionResult value back?

java.lang.NullPointerException at com.sun.faces.context.flash.ELFlash.doLastPhaseActions

I have about 3 apps in glassfish-4 using SingleSignOn for security and a 4th app for 'Access Control' where all the users for all the apps login and get redirected to any of the apps the user has access to. Everything has been working perfectly until now that i need to add a 5th app. And the 5th app refused to work with the others.
here is the code snippet i used in the first in all the apps:
#Named
#SessionScoped
public class SecurityController
// unnecessary codes ommitted
String email = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
if(email==null){
FacesContext.getCurrentInstance().getExternalContext()
.redirect("http://localhost:8080/AccessControl/login.xhtml")
}
This has been working for me in all the first 3 apps but wouldnt work in this last one. Am using standard Java EE 6 with primefaces 5.0.
Here is the error i get:
Warning: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.doLastPhaseActions(ELFlash.java:644)
at com.sun.faces.context.flash.ELFlash.doPostPhaseActions(ELFlash.java:582)
at com.sun.faces.context.ExternalContextImpl.doLastPhaseActions(ExternalContextImpl.java:1081)
at com.sun.faces.context.ExternalContextImpl.redirect(ExternalContextImpl.java:643)
Any suggestion will be greatly appreciated as this is delaying a long due move to production. Thanks

Add WebRole.cs - and have it called - in an existing ASP.NET MVC site converted to web role

I have an ASP.NET MVC 4 site running perfectly well in an Azure WebRole. The ASP.NET MVC project was started on its own, after which I added an Azure Cloud Service project to the solution and added the ASP.NET project/site as one of the 'roles' of the service (so it shows up in the 'Roles' folder).
My problem is that I would like to have working a WebRole.cs file within the ASP.NET MVC project, but no matter what I've tried to do, it appears that when deployed, it just never gets called. OnStart and the override of Run (which I know, must never leave the loop) -- these just apparently never get called.
But if you startup a new CloudService project and add, at that time from the start, an ASP.NET MVC project, it automatically has a WebRole.cs file in it, so my guess is that I need to configure something somewhere for the WebRole.cs (actually speaking, the WebRole class, which inherits RoleEntryPoint) to get called. What might that be?
using System;
using System.Web;
//using Microsoft.WindowsAzure.StorageClient;
using System.Diagnostics;
using System.Threading;
namespace Us.WebUI
{
public class WebRole : Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint
{
public override bool OnStart()
{
return true; //return base.OnStart(); // CALL THIS???
}
public override void Run()
{
while (true) {
Thread.Sleep(TimeSpan.FromSeconds(30));
try {
EmailFuncs.SendEmailToUs("An email from our WebRole?????", "Email me this, email me that.");
}
catch { }
}
}
}
}
UPDATE: Thanks, the question has been answered. But I will add: On doing this, while it clearly was working (fully deployed and in emulator), that suddenly I was having problems doing a full publish of the site. After a azure publish took 3 hours:
Verifying storage account 'xyz'... > Uploading Package... > - Updating... [stayed here for 3 hours], it failed with this error: The server encountered an internal error. Please retry the request. So one thing I was wondering is, did I need to override OnStop in WebRole.cs?
UPDATE 2: Those previous problems were fixed, and had nothing to do with this issue. Actually, I've learned this: If you ever have any warnings generated in your build, Azure often will not work with them even when they don't cause problems locally or in other hosts. Since then, I've been much more studious to tackling build warnings (but critical to this is turning off with warning codes the many warning types you want to ignore!).
Adding a class to your Web Project which inherits from RoleEntryPoint is sufficient, it should just work. Did you try setting a breakpoint in the emulator?
What you might be experiencing is that EmailFuncs.SendEmailToUs requires info from the app/web.config and that this info is not available. You need to know that your WebRole class runs in a different process (not your web application), meaning it's not using your web.config. If you want the WebRole.cs to read info from the configuration file, you'll need to add these settings in WaIISHost.exe.config

Resources