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

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.

Related

Rotativa gets 401.2 code status using windows authentication

I have an MVC 5 project which uses Windows authentication.
In order of adding pdf export support, I try to set up Rotativa lib.
The first try was Controller method
public ActionResult PrintViewToPdf()
{
return new ActionAsPdf("myAction");
}
which results as error 401.2 page in pdf file.
As i undestand (please let me know if there is another solution!!!), there is the only workaround:
public ActionResult PrintViewToPdf()
{
return new ViewAsPdf("myView");
}
In this case, I get a pdf file, but it contents a web page withount javascript scripts completed!
So, the question: is any another solution instead of replace ActionAsPdf to ViewAsPdf? If no, is it a solution to make ViewAsPdf include javascript execution results?

Equivalent Spring #Controller with #RequestMapping("/endpoint"), but using Java EE 8 only

So I'm working on implementing Oath2 authentication to allow my app to access Intuit Quickbooks company resources (items, customers, etc).
Intuit provides working examples using Spring, but I'm developing my app using JavaEE 8 with GlassFish5.
The Spring sample app callback contoller is structured as follows:
#Controller
public class CallbackController {
...
#RequestMapping("/oauth2redirect")
public String callBackFromOAuth(#RequestParam("code") String authCode, #RequestParam("state") String state, #RequestParam(value = "realmId", required = false) String realmId, HttpSession session) {
...
//after successful validation
return "connected";
This is the redirect handler controller; which address it's configured at the intuit portal (in this case, http://localhost:8080/oauth2redirect) that will be called after user approves the app and intuit will send back authorization code to this url.
So I'm a bit stuck finding what's the equivalent Spring callback redirect handler in JavaEE.
Is a #WebServlet or #WebService needed here? But then it wouldn't integrate nicely with JSF to just return the "connected" string so that it redirects to desired page (in this case connected.xhtml).
Not looking for workarounds, but the correct and standard way of implementing this on JavaEE. If you can point me to some sample apps out there or tutorials I would greatly appreciate it.
Thanks a lot in advance!
Here's full source code for callback handler controller and the full sample app.
There is at least not a really good alternative in JSF. Yes, you could 'abuse' JSF but there are other, better standards for this and this is (almost) what Spring also does. If you read the Spring Specs , you'll see the word 'Rest' being used a lot.
Well, there is a real java standard called 'Jax-RS' that is the standardized counterpart of what you do in spring.
This provides a decent analysis of the two So Jax-RS is the way to go.
But a #WebServlet or #WebService integrate perfectly with JSF. You can store any authentication information in the session and use that from JSF. No problem at all.

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.

Using hooks to trigger a process

I am trying to work out how to use the Hooks and just can't seem to get the syntax correct.
I have built a site using PirahnaCMS that has a blog component and am extending it to call some social plugins and auto post to FB, Twitter etc.
I just can't seem to get the syntax correct though. My app is MVC and I have looked at this section
1.2 ASP.NET MVC
If you're using ASP.NET MVC hooks should be attached in you Global.asax.cs in the Application_Start method, or any other place where you keep you startup code. You attach you hooks with the followin syntax:
protected void Application_Start() {
Piranha.WebPages.Hooks.Menu.RenderItemLink = (ui, str, title, url) => {
str.Append(String.Format("<span>{1}</span>", url, title)) ;
} ;
}
The Hook I believe I want to use is Piranha.WebPages.Hooks.Manager.PostEditModelAfterSave but for the life of me I can't seem to work it out.
All of the hooks are just static delegates that you can attach methods to. In the above example an anonymous method has been assigned to the hook with the syntax:
delegate += (parameters) => { method body }
You could also assign a previously declared method.
delegate += MyMethod
Example skeletons for attaching hooks should be available in the Docs at the official site. If not you can find the hooks in the file:
~/WebPages/Hooks.cs
And all delegates in:
~/Delegates.cs
I hope these URL:s are correct as I'm typing from memory :)
Regards

Cannot call Action method from UrlHelper on ASP.NET MVC 5.1

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);
}

Resources