European ASP.NET MVC Hosting

BLOG about Latest ASP.NET MVC Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET MVC 6 Hosting - HostForLIFEASP.NET :: HTTP Error 404.0 0 Not Found in MVC

clock January 29, 2021 08:32 by author Peter

The Resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I saw this error today while working on a MVC Web Project, this is a common error we get while running websites and performing any CRUD (Create, Read, Update, Delete) operation. The Stackoverflow forum is full of such queries so I decided to describe the fix here. You might not find it useful for your case but I think the majority of requests can be satisfied.

 
Let me show you an error page image here:


Look at the URL in the above image. The URL is requesting a view/page to edit the record but unfortunately the page is not found. Actually the page/view is already there but the problem is, we are not supplying the correct ID or say index to edit.
In other words we need an URL something like http://localhost:25349/demo/Edit/1 to edit the first record and http://localhost:25349/demo/Edit/2 to edit the second record. Yet in the preceding image we are not supplying the ID.
 
Let's fix it. Open the "Index" view of the "demo" controller and look at the existing code:


Oh! there is a comment instead of the ID parameter, so once you change it, such as in the following:
    <td>  
        @Html.ActionLink("Edit", "Edit", new { id=item.SM_UID }) |  
        @Html.ActionLink("Details", "Details", new { id=item.SM_UID }) |  
        @Html.ActionLink("Delete", "Delete", new { id=item.SM_UID })  
    </td>  


Your application will work fine.



ASP.NET MVC 6 Hosting - HostForLIFEASP.NET :: ActionResult In ASP.NET Core MVC

clock January 20, 2021 08:56 by author Peter
This article is an  overview of the use of ActionResult in ASP.Net Core MVC. ASP.NET Core MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. 

What is Action Method in ASP.NET Core MVC?
Actions are the methods in controller class which are responsible for returning the view or Json data. Action will mainly have return type “ActionResult” and it will be invoked from method InvokeAction called by controller. All the public methods inside a controller which respond to the URL are known as Action Methods. When creating an Action Method we must follow these rules. I have divided all the action methods into the following categories:

  1. ActionResult
  2. RedirectActionResult
  3. FileResult
  4. Security

Miscellaneous Action Results

Action Method
Description
IActionResult
Defines a contract that represents the result of an action method.
ActionResult
A default implementation of IActionResult.
ContentResult
Represents a text result.
EmptyResult
Represents an ActionResult that when executed will do nothing.
JsonResult
An action result which formats the given object as JSON.
PartialViewResult
Represents an ActionResult that renders a partial view to the response.
ViewResult
Represents an ActionResult that renders a view to the response.
ViewComponentResult
An IActionResult which renders a view component to the response.

ActionResult
The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action. IActionResult and ActionResult work as a container for other action results, in that IActionResult is an interface and ActionResult is an abstract class that other action results inherit from.

public IActionResult Index()  
{  
      return View();  

ActionResult
ActionResult is the base class of all the result type action method.

public ActionResult About()  
{  
     return View();  

ContentResult
ContentResult represents a text result. The default return type of a ContentResult is string, but it’s not limited to string. We can return any type of response by specifying a MIME type, in the code.

public ContentResult ContentResult()  
{  
      return Content("I am ContentResult");  
}  

EmptyResult
EmptyResult represents an ActionResult that when executed will do nothing. We can use it when we want to return empty result.

public EmptyResult EmptyResult() 
  
      return new EmptyResult();  

JsonResult
JsonResult formats the given object as JSON. JsonResult is use to return JSON-formatted data, it returns JSON regardless of what format is requested through Accept header. There is no content negotiation when we use JsonResult.
public JsonResult JsonResult()  
{  
      var name = "Farhan Ahmed";  
      return Json(new { data=name});  
}  

PartialViewResult
PartialViewResult represents an ActionResult that renders a partial view to the response. PartialViews are essential when it comes to loading a part of page through AJAX, they return raw rendered HTML.


public PartialViewResult PartialViewResult()  
{  
      return PartialView("_PartialView");  
}  

ViewResult
ViewResult represents an ActionResult that renders a view to the response. It is used to render a view to response, we use it when we want to render a simple .cshtml view.

public ViewResult ViewResult()  
{  
      return View("About","Home");  
}  

ViewComponentResult
ViewComponentResult is an IActionResult which renders a view component to the response. We use view component by calling Component.InvokeAsync in the view. We can use it to return HTML form a view component. If we want to reuse our business logic or refresh the HTML parts of the page that are loaded with view component we can do that using view component.

public ViewComponent ViewComponent()  
{  
       return ViewComponent();  

Action results from previous version of ASP.NET MVC that are either renamed or deleted.
  • JavaScriptResult - This action result does not exist anymore we can use ContentResult.
  • FilePathResult - We can use VirtualFileResult or PhysicalFileResult instead of FilePathResult
  • HttpNotFoundResult - We can use NotFoundResult instead HttpNotFoundResult
  • HttpStatusCodeResult - We can use StatusCodeResult instead HttpStatusCodeResult
  • HttpUnauthorizedResult - We can use UnauthorizedResult instead HttpUnauthorizedResult


ASP.NET MVC 6 Hosting - HostForLIFEASP.NET :: Redirect Action Result in ASP.NET Core MVC

clock January 15, 2021 08:56 by author Peter

This article overviews redirect action results in ASP.NET Core MVC. We will lean all redirect action results step by step with examples.

There are four types of redirect action results in ASP.Net Core MVC. Each one can either return normal redirect or permanent. The return method related to the permanent one is suffixed with the Permanent keyword. You can also return these results with their Permanent property set to true. These action results are:
    RedirectResult
    RedirectToActionResult
    RedirectToRouteResult
    LocalRedirectResult

RedirectResult
RedirectResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header to the supplied URL. It will redirect us to the provided URL, it doesn’t matter if the URL is relative or absolute.
    public RedirectResult MyProfile()  
    {  
       return Redirect("https://www.c-sharpcorner.com/members/farhan-ahmed24");  
    }  
      
    public RedirectResult Profile()  
    {  
       return RedirectPermanent("https://www.c-sharpcorner.com/members/farhan-ahmed24");  
    }  


If we call the RedirectPermanent method, it redirect us permanently. Also as I explained in previous section we don’t need to use these methods to redirect permanently or temporarily, we can just new up an instance of RedirectResult with its Permanent property set to true or false and return that instead.
    return new RedirectResult("/") { Permanent = true};  

RedirectToActionResult

RedirectToActionResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. It targets a controller action, taking in action name, controller name, and route value.
    public RedirectToActionResult EmployeeList()  
    {  
        return RedirectToAction("Index", "Employees");  
    }  


RedirectToRouteResult
RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route. It takes a route name, route value and redirect us to that route with the route values provided.
    public RedirectToRouteResult DepartmentList()  
    {  
        return RedirectToRoute(new { action = "Index", controller = "Departments", area="" });  
    }  


LocalRedirectResult

LocalRedirectResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header to the supplied local URL. We should use LocalRedirectResult if we want to make sure that the redirects occur in some context that are local to our site. This action result type takes a string for URL needed for redirect, and a bool flag to tell it if it’s permanent. Under the hood, it checks the URL with the Url.IsLocalUrl("URL") method to see if it’s local. If it redirects us to the address, but if it wasn’t, it will throw an InvalidOperationException. You cannot pass a local URL with an absolute address like this, http://localhost:52513/Home/Index, you’ll get an exception. That’s because the IsLocalUrl method considers a URL like this to not be local, so you must always pass a relative URL in.
    public LocalRedirectResult LocalRedirect()  
    {  
        return LocalRedirect("/Home/Index");  
    }  

    public LocalRedirectResult LocalRedirectActionResult()  
            {  
                var IsHomeIndexLocal = Url.IsLocalUrl("/Home/Index");  
                var isRootLocal = Url.IsLocalUrl("/");  
      
                var isAbsoluteUrlLocal = Url.IsLocalUrl("http://localhost: 52513/Home/Index");  
                return LocalRedirect("/Home/Index");  
            }  

Step 1
Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next.
 
Step 2
Name the project FileResultActionsCoreMvc_Demo and click Create.
 
Step 3
Select Web Application (Model-View-Controller), and then select Create. Visual Studio used the default template for the MVC project you just created.
 
Step 4
In Solution Explorer, right-click the wwwroot folder. Select Add > New Folder. Name the folder Files. Add some files to work with them.
 
Complete controller code
    using System;  
    using System.Collections.Generic;  
    using System.Diagnostics;  
    using System.Linq;  
    using System.Threading.Tasks;  
    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.Extensions.Logging;  
    using RedirectResultActionsCoreMvc_Demo.Models;  
      
    namespace RedirectResultActionsCoreMvc_Demo.Controllers  
    {  
        public class HomeController : Controller  
        {  
            public IActionResult Index()  
            {  
                return View();  
            }  
      
            public RedirectResult MyProfile()  
            {  
                return Redirect("https://www.c-sharpcorner.com/members/farhan-ahmed24");  
            }  
            public RedirectResult Profile()  
            {  
                return RedirectPermanent("https://www.c-sharpcorner.com/members/farhan-ahmed24");  
            }  
      
            public RedirectToActionResult EmployeeList()  
            {  
                return RedirectToAction("Index", "Employees");  
            }  
      
            public RedirectToRouteResult DepartmentList()  
            {  
                return RedirectToRoute(new { action = "Index", controller = "Departments", area="" });  
            }  
      
            public LocalRedirectResult LocalRedirect()  
            {  
                return LocalRedirect("/Home/Index");  
            }  
        }  
    }  


Step 5
Open Index view which is in the views folder under the Home folder. Add the below code in Index view.
 
Index View
    @{  
        ViewData["Title"] = "Home Page";  
    }  
      
    <h3 class="text-uppercase">RedirectResult Action in core mvc</h3>  
    <ul class="list-group list-group-horizontal">  
        <li class="list-group-item"><a asp-action="MyProfile" asp-controller="Home" target="_blank">Redirect Result</a></li>  
        <li class="list-group-item"><a asp-action="EmployeeList" asp-controller="Home" target="_blank">RedirectToActionResult</a></li>  
        <li class="list-group-item"><a asp-action="DepartmentList" asp-controller="Home" target="_blank">RedirectToRouteResult</a></li>  
        <li class="list-group-item"><a asp-action="LocalRedirect" asp-controller="Home" target="_blank">LocalRedirectResultt</a></li>  
    </ul>  

Step 6

Build and run your Project, ctrl+F5



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Tag cloud

Sign in