Value cannot be null. Parameter name: view - asp.net-mvc-4

I was trying to render a dynamic view into string.I got the idea from here and it is working well locally but on hosting I am getting the following error.My attempt is to convert the string to bytes for sending email
Value cannot be null. Parameter name: view
Controller
public ActionResult Index(int RegId)
{
try
{
var _mdlReceiptPdf = new ReceiptPdfVM
{
...
...
};
string result = RazorViewToString.RenderRazorViewToString(this, Server.MapPath("~/Views/Shared/_Receipts.cshtml"), _mdlReceiptPdf);
StringReader _sr = new StringReader(result);
return Json(result, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(ex.Message, JsonRequestBehavior.AllowGet);
}
}
Util.cs
public static class RazorViewToString
{
public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
}

I had this problem. In my case, it turned out that the partial view wasn't being uploaded to the hosted website because in the File Properties for the partial view, the BuildAction was set to None instead of Content

Yeah looks like this error is saying view can't be found.
In my case I had a typo for the view name in the controller.
_QuickView.cshtml instead of QuickView.cshtml

Related

Blazor Server - Return Count from SQL - Returns System.Collections.Generic.List`1[System.Object]

I'm trying to return a count of row's from a stored procedure in SQL. I've been working at this for hours now. I'm at a complete loss.
SQL Code: Returns result of 49 when run in SQL.
begin
select Count (EmployeeID)
from dbo.Employees
Where (Employees.Active = 'True')
end
Component Code:
#attribute [Authorize(Roles = "Admin")]
#using System.Collections;
#using WahlenDataAccessLibrary;
#using WahlenDataAccessLibrary.Models;
#using BlazorApp1.Models;
#inject IEmployeesData employeeData;
<div class="widget">
<h5 class="widget--title">Active Employees</h5>
<div class="widget--body">
<p class="widget--number">
<span>#employeeCount</span>
Active Employees
</p>
</div>
</div>
#code {
private string employeeCount { get; set; }
//private IEmployeeModel employeeCount = new EmployeeModel();
protected override async Task OnInitializedAsync()
{
var count = await employeeData.EmployeeCount();
//string employeeCount = await employeeData.EmployeeCount();
string employeeCount = count.ToString();
Console.WriteLine(employeeCount);
if (employeeCount != null)
{
Console.WriteLine("generic value");
}
else
{
Console.WriteLine("no value");
}
}
}
DataFile Code: To get the value from stored procedure.
public async Task<string> EmployeeCount()
{
var employeeCount = await _db.LoadValue("dbo.spWidget_EmployeeCount", "DefaultConnection");
return employeeCount.ToString();
}
}
The DataFile where 'LoadValue' is used. This is linked back to my SqlDataAccess File which uses this code.
public async Task<string> LoadValue(string storedProcedure, string connectionStringName)
{
string connectionString = _config.GetConnectionString(connectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
var data = await connection.QueryAsync(storedProcedure,
commandType: CommandType.StoredProcedure);
return data.ToString();
}
}
When the application is running the console writes.
System.Collections.Generic.List`1[System.Object]
no value
The
System.Collections.Generic.List`1[System.Object]
comes from
var data = await connection.QueryAsync(storedProcedure,
commandType: CommandType.StoredProcedure);
return data.ToString();
the var data is actually an object. Callint .ToString() on a object will print out the type of the object (if the method is not over written).
Please check if the QueryAsync has a generic version. Usually it does and will make a type cast for you in the background. Try something like QueryAsync<int>. Or try to find the rigth method (with a single return item, instead of a list) on your ORM/db provider.
From the current context it is not possible to tell what db provider you are using.
I'm going to update the code here, as I've got it working.
Task<IEnumerable> was more so the answer here. If you have an sql query that is simply counting rows, this is how you access that data on Blazor on your front end. You can return the value as a string.
My sqlaccess file code now looks like this.
public async Task<IEnumerable<int>> LoadValue(string storedProcedure, string connectionStringName)
{
string connectionString = _config.GetConnectionString(connectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
var data = await connection.QueryAsync<int>(storedProcedure,
commandType: CommandType.StoredProcedure);
return data;
}
}
And this is on the front end component.
private string employeeCount { get; set; }
protected override async Task OnInitializedAsync()
{
employeeCount = await employeeData.EmployeeCount();
Console.WriteLine(employeeCount);
if (employeeCount != null)
{
Console.WriteLine("generic value");
}
else
{
Console.WriteLine("no value");
}

Accessing HttpContext.Session from static method

I am getting following error when accessing HttpContext.Session from static method placed in separate task:
Session has not been configured for this application or request.
I used this article to implement access to HttpContext outside the controller
From controller I invoke this static method that used to retrieve image data:
public static void CreateDummyGallery(Gallery gallery)
{
Logger.LogDebug(LogModule.Dummy, $"Starting gallery creation.");
Task.Factory.StartNew(() =>
{
try
{
List<DummyPicture> pictures;
using (var context = new MyzeumContext())
{
int top = 10;
pictures = context.DummyPictures.FromSql($"SELECT * FROM dummypictures ORDER BY RAND() LIMIT {top}").ToList();
}
Logger.LogDebug(LogModule.Dummy, $"Starting retrieving images.");
Parallel.ForEach(pictures, picture => {
using (WebClient client = new WebClient())
{
}
});
Logger.LogDebug(LogModule.Dummy, $"Done retrieving images.");
}
catch(Exception e)
{
Logger.LogError(LogModule.Server, e.Message, e);
}
});
}
The problem occurs in Logger.LogDebug() because this is where I access HttpContext:
public void LogDebug(LogModule module, string message, Exception stackTrace = null)
{
Log record = new Log();
record.Module = module;
record.ThreadId = Environment.CurrentManagedThreadId;
record.SessionId = HttpContextHelper.Current?.Session?.Id;
record.Message = message;
record.Logged = DateTime.UtcNow;
if(stackTrace != null)
{
record.Message += $" :{stackTrace.StackTrace}";
}
queue.Enqueue(record);
}
The problem 99% occurs in the first call inside task:
Logger.LogDebug(LogModule.Dummy, $"Starting retrieving images.");
BUT, right after application starts this whole task block works fine and does not throw any exception. Problem starts after following requests.

How to display error message from exception without redirecting to new view in MVC 4

I would like to catch exceptions that might bubble up from a class library method to the action from which it was called and would like to display the error in the view without actually redirecting to a new view. I've overriden the OnException method in my controller and have created a partial view to display the error but when an error occurs the partial view is not being rendered where I want it to show but rather where I have a table and it will be replaced with the error message which is not what I wan, not to mention the table is in a different place in the view than where the error should display. I haven't done much exception handling with MVC so I don't know if my approach is completely wrong.
My code for the exception
protected override void OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = true;
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var exp = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
//Assigns error message to property for message in view model
ReceivngFormViewModel viewModel = new ReceivngFormViewModel
{
ErrorMessage = exp.Exception.Message
};
filterContext.Result = new PartialViewResult()
{
//_Error is the partial view
ViewName = "_Error",
ViewData = new ViewDataDictionary(viewModel)
};
}
this code help you use ModelState.AddModelError
**Controller**
Model obj=new Model();
try{
}
cathch(exception ex){
ModelState.AddModelError("objectname", ex.tostring());
}
return obj;
**Viwe**
#Html.ValidationMessageFor(model => model.objectname)

Error on Rendering a View to a String

I am trying to render a view to a string in ASP Net Core 1 but keep receiving the error:
Operator '!' cannot be applied to operand of type ''
on this line:
view.RenderAsync(viewContext).GetAwaiter().GetResult();
I am confused as to why this might be the case and looking for some assistance.
Alot of the examples and help I have seen in this area relate to RC1. We are using the final core 1.0 here.
I'm using the following code which i found online:
public ViewRender(
IRazorViewEngine viewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public string Render<TModel>(string name, TModel model, ActionContext actionContext)
{
var viewEngineResult = _viewEngine.FindView(actionContext, name, true);
if (!viewEngineResult.Success)
{
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
}
var view = viewEngineResult.View;
using (var output = new StringWriter())
{
var viewContext = new ViewContext(
actionContext,
view,
new ViewDataDictionary<TModel>(
metadataProvider: new EmptyModelMetadataProvider(),
modelState: new ModelStateDictionary())
{
Model = model
},
new TempDataDictionary(
actionContext.HttpContext,
_tempDataProvider),
output,
new HtmlHelperOptions());
view.RenderAsync(viewContext).GetAwaiter().GetResult();
return output.ToString();
}
}
}

Credentials prompted while rendering a remote ReportViewer control in MVC4

I am creating one web app (mvc 4) to authorize customers (using membership provider) to view the reports(SSRS 2008) for which they are registered but they don't have any kind of access to our report server.
Based on the link How do I render a remote ReportViewer aspx page in MVC4?, I have implemented Elsimer's latest answer and it works well in downloading as a pdf file.
But when I try to render as html using the same code mentioned in the above link it is asking for the windows credentials to access the report server.
So I am giving a general credential which has all access to all the reports in the reportserver through the code. but it is still asking for the credentials for the report server when they try to view as html in the client side browser. Report is getting rendered but the images and graphs are not rendering without credentials.
Please advise, I have tried many things to solve this. but no luck.
My controller and credential class code as follows:
[Route("report/MonthlySummary")]
[ValidateAntiForgeryToken]
public ActionResult MonthlySummary(MonthlyReportParameters model)
{
if (ModelState.IsValid)
{
try
{
var actionType = model.ActionType;
if (actionType == "View Report")
{
return ExportMonthlyReportToHtml(model);
}
else if (actionType == "Download pdf report")
{
return ExportMonthlyReportToPdf(model);
}
}
catch (Exception ex)
{
//Logging errors
}
}
return null;
}
private ActionResult ExportMonthlyReportToHtml(MonthlyReportParameters monthlyParams)
{
ReportViewer reportViewer = BuildMonthlyReport(monthlyParams);
reportViewer.ServerReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
//To view the report in html format
streamBytes = reportViewer.ServerReport.Render("HTML4.0", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
var htmlReport = File(streamBytes, "text/html");
return htmlReport;
}
private static ReportViewer BuildMonthlyReport(MonthlyReportParameters model)
{
ReportViewer reportViewer = new Microsoft.Reporting.WebForms.ReportViewer();
try
{
var rptParameters = new List<ReportParameter>
{
//Building parameters
};
reportViewer.ProcessingMode = ProcessingMode.Remote;
reportViewer.ServerReport.ReportPath = "/reportFolder/reportName";
var reportServerUrl = ConfigurationManager.AppSettings["ReportServerUrl"];
if(!string.IsNullOrEmpty(reportServerUrl))
{
reportViewer.ServerReport.ReportServerUrl = new Uri(reportServerUrl);
}
reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
reportViewer.ServerReport.SetParameters(rptParameters);
}
catch (Exception ex)
{
var errorMessage = ex.Message;
//TODO: handle errors;
}
return reportViewer;
}
public sealed class ReportServerCredentials : IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = null;
password = null;
authority = null;
return false;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}
public ICredentials NetworkCredentials
{
get
{
string userName = ConfigurationManager.AppSettings["ReportUserName"];
if ((string.IsNullOrEmpty(userName)))
{
throw new Exception("Missing user name from web.config file");
}
string password = ConfigurationManager.AppSettings["ReportPassword"];
if ((string.IsNullOrEmpty(password)))
{
throw new Exception("Missing password from web.config file");
}
string domain = ConfigurationManager.AppSettings["DomainName"];
if ((string.IsNullOrEmpty(domain)))
{
throw new Exception("Missing domain from web.config file");
}
return new NetworkCredential(userName, password, domain);
}
}
}
Thanks in advance,