Babak Sekandari
asked on
How do I change the function that gets called in the controller of an ASP.NET MVC app when AngularJS is involved
I have an ASP.NET MVC framework 4.5 with AngularJS that I’ve inherited and have to maintain.
The application had a .cshtml to PDF capability for a summary of all student questionnaires for a given term. The questions changed, and so we added a new .cshtml page for the new questions and renamed the .cshtml for the old questions.
Now there are
PrintSummary.cshtml
and
PrintSummaryBefore2019.csh tml.
What I’m trying to do is to have a condition somewhere in the code that will call PrintSummaryBefore2019.csh tml for queries before 2019 and call PrintSummary.cshtml for queries starting with 2019 and after.
The anchor tag for the Print To PDF functionality was originally coded like this:
Then in the PrintSummaryPDF() function of the Survey controller, the previous programmer had code that parsed the Url, altered it to call a different function in the Survey controller, and then started a process like this:
As you can see from above, the PrintSummary() function of the Survey controller will get called.
I added code to replace the Url based on the term, like this:
Strangely, even though I step through the debugger and see that it is hitting the correct condition, the PrintSummaryBefore2019() function in the Survey controller never gets hit in the debugger.
How do I change the function that gets called in the controller of an MVC app when AngularJS is involved and when the function is already being altered within controller as shown above?
The application had a .cshtml to PDF capability for a summary of all student questionnaires for a given term. The questions changed, and so we added a new .cshtml page for the new questions and renamed the .cshtml for the old questions.
Now there are
PrintSummary.cshtml
and
PrintSummaryBefore2019.csh
What I’m trying to do is to have a condition somewhere in the code that will call PrintSummaryBefore2019.csh
The anchor tag for the Print To PDF functionality was originally coded like this:
<div style="padding-bottom: 10px; text-align: right;">
<a class="btn btn-danger" href="../Survey/PrintSummaryPDF?Term={{term.Term}}&Campus={{campus.CampusId}}&Faculty={{faculty}}&Dept={{dept}}" target="_blank"><span class="glyphicon glyphicon-print"></span> Bulk Print </a>
</div>
I changed it to this: <a class="btn btn-danger" onclick="setHref" target="_blank"></a>
And I added this JavaScript:<script>
function setHref(term, campusid, faculty, dept) {
var term = document.getElementById("term");
if (Number(term) <= 20190)
window.location.href = "../Survey/PrintSummaryPDFBefore2019?Term=" + term +
"&Campus=" + campusid +
"&Faculty=" + faculty +
"&Dept=" + dept ;
else
window.location.href = "../Survey/PrintSummaryPDF?Term=" + term +
"&Campus=" + campusid +
"&Faculty=" + faculty +
"&Dept=" + dept ;
return false;
}
</script>
But the debugger never even reaches this script. It seems that if a page is setup for AngularJS then it won’t parse regular JavaScript calls; I don’t know.Then in the PrintSummaryPDF() function of the Survey controller, the previous programmer had code that parsed the Url, altered it to call a different function in the Survey controller, and then started a process like this:
[AllowAnonymous]
public ActionResult PrintSummaryPDF()
{
string fileNameDatePart = DateTime.Now.ToLongTimeString().Replace(":", string.Empty).Replace(" ", string.Empty);
string pdfFileName = "Survey" + fileNameDatePart + ".pdf";
var outputPath = ConfigurationManager.AppSettings["WkHtmlToPdfOutputPath"] + pdfFileName;
Process process = null;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = ConfigurationManager.AppSettings["WkHtmlToPdfExePath"];
processStartInfo.Verb = "runas";
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.Arguments = HttpContext.Request.Url.AbsoluteUri.Replace("PrintSummaryPDF", "PrintSummary") + " " +
" --load-error-handling ignore" + " --cookie .ASPXAUTH " + Request.Cookies["DCCCDCookie"].Value + " \"" + outputPath + "\"";
process = Process.Start(processStartInfo);
process.WaitForExit(Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"]));
int exitCode = process.ExitCode;
string stdout = process.StandardOutput.ReadToEnd();
string stderr = process.StandardError.ReadToEnd();
return File(outputPath, "application/pdf");
}
As you can see from above, the PrintSummary() function of the Survey controller will get called.
I added code to replace the Url based on the term, like this:
string[] query = HttpContext.Request.Url.Query.Split('&');
int equalSignIndex = query[0].IndexOf('=');
int termAsNum;
string term = query[0].Substring(equalSignIndex + 1);
int.TryParse(term, out termAsNum);
if (termAsNum >= 20191)
{
processStartInfo.Arguments = HttpContext.Request.Url.AbsoluteUri.Replace("PrintSummaryPDF", "PrintSummary") + " " +
" --load-error-handling ignore" + " \"" + outputPath + "\"";
}
else
{
processStartInfo.Arguments = HttpContext.Request.Url.AbsoluteUri.Replace("PrintSummaryPDF", "PrintSummaryBefore2019") + " " +
" --load-error-handling ignore" + " \"" + outputPath + "\"";
}
Strangely, even though I step through the debugger and see that it is hitting the correct condition, the PrintSummaryBefore2019() function in the Survey controller never gets hit in the debugger.
How do I change the function that gets called in the controller of an MVC app when AngularJS is involved and when the function is already being altered within controller as shown above?
ASKER
Here's what I'm trying so far:
All I did was change:
All I did was change:
<a class="btn btn-danger" href="../Survey/PrintSummaryPDF?Term={{term.Term}}&Campus={{campus.CampusId}}&Faculty={{faculty}}&Dept={{dept}}" target="_blank"><span class="glyphicon glyphicon-print"></span> Bulk Print </a>
to<a class="btn btn-danger" href="../Survey/{{returnConditionalDisplayLocation()}}?Term={{term.Term}}&Campus={{campus.CampusId}}&Faculty={{faculty}}&Dept={{dept}}" target="_blank"><span class="glyphicon glyphicon-print"></span> Bulk Print </a>
and I added this:
$scope.returnConditionalDisplayLocation = function returnConditionalDisplayLocationFn() {
if ($scope.term.Term <= 20190)
return "PrintSummaryPDFBefore2019";
else
return "PrintSummaryPDF";
}
I'm testing to see if this works.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Using ng-href instead of href helped. Thanks.
ASKER