Monday, January 16, 2023

Friday, November 27, 2020

HTTP Error 502.5 - Process Failure or core run time error

When deploying an ASP.NET Core application to IIS, you may find IIS returning HTTP Error 502.5 instead of your web page


 https://www.danielcrabtree.com/blog/400/how-to-fix-iis-http-error-502-5-in-asp-net-core#:~:text=The%20HTTP%20Error%20502.5%20%2D%20Bad,Core%20Runtime%20is%20not%20installed

Tuesday, October 22, 2019

Fill dropdown in razor pages in asp.net core

fill dropdown in razor pages in asp.net core or fill dropdown in razor or core or fill dropdown in razor pages in core




C# Code


public SelectList mItem_List2 { get; set; }
        public List<SelectListItem> mItem_List { get; set; }
        public List<SelectListItem> Options { get; set; }
        public IActionResult OnGet()
        {

         

            var data4 = from x in _context.mItem
                        orderby x.IName
                        select (new SelectListItem() { Value = x.mIcode, Text = x.IName + ", " + x.Item_ID });

            mItem_List = data4.ToList();

            Options = _context.mItem.Select(a =>
                                  new SelectListItem
                                  {
                                      Value = a.mIcode,
                                      Text =  a.IName+", "+a.Item_ID
                                  }).ToList(); 


            object selectedItem = null;
            var data5 = from x in _context.mItem
                        orderby x.IName
                        select x;
            mItem_List2 = new SelectList(data5.AsNoTracking(), "mIcode", "IName", selectedItem);
           
            return Page();
        }



Html code

<select asp-for="ItemI.mIcode" class="form-control"
                        asp-items="@Model.Options">
                    <option value="">-- Select Department --</option>
                </select>
                <select asp-for="ItemI.mIcode" class="form-control"
                        asp-items="@Model.mItem_List">
                    <option value="">-- Select Department --</option>
                </select>
                <select asp-for="ItemI.mIcode" class="form-control"
                        asp-items="@Model.mItem_List2">
                    <option value="">-- Select Department --</option>
                </select>

Wednesday, October 9, 2019

MVC Core Session Authorization

 MVC Core Session Authorization or Session Authorization or Session Authorize or  or Core Session Authorization




using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace WebApplication6.Auth
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class CustomAuthorization : Attribute, IAuthorizationFilter
    {

        public void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            var ses = filterContext.HttpContext.Session.GetString("userid");

            if (ses == null || ses == "")
            {
                //filterContext.HttpContext.Response.Redirect("/Home/Login");

                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        //{"Controller","Login" }
                        {"Controller","Login" },
                        {"Action","Index" }
                    }
                    );
                return;
            }

        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class CustomAuthorizationRole : Attribute, IAuthorizationFilter
    {
        string forms_; string actions_;
        public CustomAuthorizationRole(string forms, string actions)
        {
            forms_ = forms;
            actions_ = actions;
        }
        public void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            if (forms_.ToLower() == "login" && actions_.ToLower() == "view") { }
            else
            {
                //filterContext.HttpContext.Response.Redirect("/Home/Login");

                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        {"Controller","Home" },
                        {"Action","AccessDinied" }
                    }
                    );
                return;
            }

        }
    }
}

Sunday, October 6, 2019

Call Constructor Api by controller or Call async Api by controller or Call async method or get async method or call async web api, call core api

Call Constructor Api by controller or Call async Api by controller or Call async method or get async method or call async web api, call core api


1.Web Api
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Data;
using WebApplication8.Models;

namespace WebApplication8.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class customers_apiController : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        public customers_apiController(ApplicationDbContext context)
        {
            _context = context;
        }
        // GET: api/customers_api
        [HttpGet]
        public async Task<List<customer>> Getcustomer([FromQuery] customer d)
        {
            var data = from x in _context.customer
                       where (d.Cust_ID == null || d.Cust_ID == x.Cust_ID)
                       && (d.Cust_Name == null || x.Cust_Name.Contains(d.Cust_Name))
                       orderby x.Cust_Name
                       select x;
            return await data.ToListAsync();
        }
    }
}

2. Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Data;
using WebApplication8.Models;

namespace WebApplication8.Controllers
{
    public class customersController : Controller
    {
        private readonly ApplicationDbContext _context;       
        customers_apiController api;
        public customersController(ApplicationDbContext context)
        {
            _context = context;
            api = new customers_apiController(_context);
        }
        // GET: customers     
        public async Task<IActionResult> Index(customer d)
        {           
            List<customer> data = await api.Getcustomer(d);
            return View(data);
        }
    }
}

Wednesday, October 2, 2019

Authorizaton Filter In razor pages







namespace WebApplicationRazor.Data
{
    public class custom_filter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
        }
    }

    public class CustomFilterAttribute : ResultFilterAttribute
    {

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            //if (context.HttpContext.Session["user"] == null || context.HttpContext.Session["user"] == "")
            context.HttpContext.Response.Redirect("/");
            base.OnResultExecuting(context);
        }
        public override Task OnResultExecutionAsync(ResultExecutingContext context
            , ResultExecutionDelegate next)       
        {
            context.HttpContext.Response.Redirect("/");
            return base.OnResultExecutionAsync(context, next);
        }
    }
}

Multiple Button on Razor page

1. Multiple Button on Razor page or Multiple Button on form tag with different action



a.  cshtml page

@page
@model WebApplicationRazor.Pages.Customer2.index2Model
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>index2</title>
</head>
<body>
    Rinkesh<br>
    @ViewData["msg"]
    <br>
    <form method="post">
        <div>Slill you know </div>
        <input type="submit" value="Jave" asp-page-handler="JavaSkill" asp-route-sessioncount="30" />
        <br>
        <input type="submit" value="PHP" asp-page-handler="PhpSkill" asp-route-sessioncount="20" />
        <br>
        <input type="submit" value="CPP" asp-page-handler="CPPSkill" asp-route-sessioncount="5" />
    </form>
</body>
</html>



b.  cshtml.cs page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplicationRazor.Pages.Customer2
{
    public class index2Model : PageModel
    {
        public void OnGet()
        {

        }

        public void OnPostJavaSkill(int sessionCount)
        {
            ViewData["msg"] = $"Your query for  { sessionCount } is processed.";
        }
        public void OnPostPHPSkill(int sessionCount)
        {
            ViewData["msg"] = $"Your query for  { sessionCount } is processed.";
        }
        public void OnPostCPPSkill(int sessionCount)
        {
            ViewData["msg"] = $"Your query for  { sessionCount } is processed.";
        }
    }
}