Passing TempData with RedirectToAction works in Visual Studio but not in production
Web Application ASP MVC Core 2.1
I have two models with a Parent-Children relationship, they are Course and CourseEdition; a Course can have many Editions.
The page where I display Course details also shows a list of its editions.
I use JQuery to populate the same page with the details of the edition: when clicking on one of the editions I have a DIV filled with its details data.
There are actions (i.e. enroll one or more person in an edition) that I would like to end with a redirect to the Editions details.
I have set up a mechanism like this:
In the controller action "RegisterPeople" I set TempData with the ID of the Edition I want to be redirected to, then I redirect to the Details action in CourseController.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterEmployees([Bind("CourseId,CourseEditionId,Subscribers")] RegistrationData regData)
{
TempData["NextCourseId"] = regData.CourseId;
TempData["NextEditionId"] = regData.CourseEditionId;
if (ModelState.IsValid)
{
<do something>
return RedirectToAction("Details", "Courses", new { id = regData.CourseId });
}
Here (Course Controller, Details Action) I read TempData and copy its value in ViewBag.
public async Task<IActionResult> Details(int? id)
{
<do something>
ViewBag.GotoEditionId = TempData["NextEditionId"] ;
return View();
}
In the details view I have a DIV with a data-attribute populated by the ViewBag value (the Edition's ID),
<div id="GotoEditionId" data-ceid="@ViewBag.GotoEditionId"> </div>
@section Scripts {
@{await Html.RenderPartialAsync("_CourseScripts");}
}
Now, in _CourseScript.cshtml a JQuery event handler is triggered when clicking on this div does the magic of populating the edition div.
$(document).ready(function () {
$(document).on("click", "#GotoEditionId", function (e) {
e.preventDefault();
var params = {};
params["id"] = $(this).data("ceid");
$.ajax({
url: $("#GetEditionUrl").data('get-edition'),
type: "GET",
data: params,
success: function (result) {
$("#EditionBlock").html(result);
},
error: function (err) {
alert("error");
$("#EditionBlock").html(err.responseText);
}
});
});
var GotoEditionId = $("#GotoEditionId").data("ceid");
if (GotoEditionId > 0) {
$("#GotoEditionId").trigger("click");
}
})
This works perfectly while I am in development with Visual Studio.
When I deploy to a remote IIS server, TempData is filled in the "register" action but it is found empty when entering in the "Details" action.
Who is the culprit? Why TempData is persisted in dev mode and vanishes in production?
EDIT 1
Using Session
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider()
.AddJsonOptions(config =>
{
config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSession();
.....
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
jquery visual-studio-2017 asp.net-core-mvc tempdata asp.net-core-mvc-2.1
add a comment |
Web Application ASP MVC Core 2.1
I have two models with a Parent-Children relationship, they are Course and CourseEdition; a Course can have many Editions.
The page where I display Course details also shows a list of its editions.
I use JQuery to populate the same page with the details of the edition: when clicking on one of the editions I have a DIV filled with its details data.
There are actions (i.e. enroll one or more person in an edition) that I would like to end with a redirect to the Editions details.
I have set up a mechanism like this:
In the controller action "RegisterPeople" I set TempData with the ID of the Edition I want to be redirected to, then I redirect to the Details action in CourseController.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterEmployees([Bind("CourseId,CourseEditionId,Subscribers")] RegistrationData regData)
{
TempData["NextCourseId"] = regData.CourseId;
TempData["NextEditionId"] = regData.CourseEditionId;
if (ModelState.IsValid)
{
<do something>
return RedirectToAction("Details", "Courses", new { id = regData.CourseId });
}
Here (Course Controller, Details Action) I read TempData and copy its value in ViewBag.
public async Task<IActionResult> Details(int? id)
{
<do something>
ViewBag.GotoEditionId = TempData["NextEditionId"] ;
return View();
}
In the details view I have a DIV with a data-attribute populated by the ViewBag value (the Edition's ID),
<div id="GotoEditionId" data-ceid="@ViewBag.GotoEditionId"> </div>
@section Scripts {
@{await Html.RenderPartialAsync("_CourseScripts");}
}
Now, in _CourseScript.cshtml a JQuery event handler is triggered when clicking on this div does the magic of populating the edition div.
$(document).ready(function () {
$(document).on("click", "#GotoEditionId", function (e) {
e.preventDefault();
var params = {};
params["id"] = $(this).data("ceid");
$.ajax({
url: $("#GetEditionUrl").data('get-edition'),
type: "GET",
data: params,
success: function (result) {
$("#EditionBlock").html(result);
},
error: function (err) {
alert("error");
$("#EditionBlock").html(err.responseText);
}
});
});
var GotoEditionId = $("#GotoEditionId").data("ceid");
if (GotoEditionId > 0) {
$("#GotoEditionId").trigger("click");
}
})
This works perfectly while I am in development with Visual Studio.
When I deploy to a remote IIS server, TempData is filled in the "register" action but it is found empty when entering in the "Details" action.
Who is the culprit? Why TempData is persisted in dev mode and vanishes in production?
EDIT 1
Using Session
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider()
.AddJsonOptions(config =>
{
config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSession();
.....
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
jquery visual-studio-2017 asp.net-core-mvc tempdata asp.net-core-mvc-2.1
1
What is yourStartup.cs
? Have you acceptConsentCookie
while accessing the published site? ForTempData
, it usesCookies
orSessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and addTempData["Test"]
to HomeController and access it fromAbout
action.
– Tao Zhou
Nov 22 '18 at 5:44
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
1
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51
add a comment |
Web Application ASP MVC Core 2.1
I have two models with a Parent-Children relationship, they are Course and CourseEdition; a Course can have many Editions.
The page where I display Course details also shows a list of its editions.
I use JQuery to populate the same page with the details of the edition: when clicking on one of the editions I have a DIV filled with its details data.
There are actions (i.e. enroll one or more person in an edition) that I would like to end with a redirect to the Editions details.
I have set up a mechanism like this:
In the controller action "RegisterPeople" I set TempData with the ID of the Edition I want to be redirected to, then I redirect to the Details action in CourseController.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterEmployees([Bind("CourseId,CourseEditionId,Subscribers")] RegistrationData regData)
{
TempData["NextCourseId"] = regData.CourseId;
TempData["NextEditionId"] = regData.CourseEditionId;
if (ModelState.IsValid)
{
<do something>
return RedirectToAction("Details", "Courses", new { id = regData.CourseId });
}
Here (Course Controller, Details Action) I read TempData and copy its value in ViewBag.
public async Task<IActionResult> Details(int? id)
{
<do something>
ViewBag.GotoEditionId = TempData["NextEditionId"] ;
return View();
}
In the details view I have a DIV with a data-attribute populated by the ViewBag value (the Edition's ID),
<div id="GotoEditionId" data-ceid="@ViewBag.GotoEditionId"> </div>
@section Scripts {
@{await Html.RenderPartialAsync("_CourseScripts");}
}
Now, in _CourseScript.cshtml a JQuery event handler is triggered when clicking on this div does the magic of populating the edition div.
$(document).ready(function () {
$(document).on("click", "#GotoEditionId", function (e) {
e.preventDefault();
var params = {};
params["id"] = $(this).data("ceid");
$.ajax({
url: $("#GetEditionUrl").data('get-edition'),
type: "GET",
data: params,
success: function (result) {
$("#EditionBlock").html(result);
},
error: function (err) {
alert("error");
$("#EditionBlock").html(err.responseText);
}
});
});
var GotoEditionId = $("#GotoEditionId").data("ceid");
if (GotoEditionId > 0) {
$("#GotoEditionId").trigger("click");
}
})
This works perfectly while I am in development with Visual Studio.
When I deploy to a remote IIS server, TempData is filled in the "register" action but it is found empty when entering in the "Details" action.
Who is the culprit? Why TempData is persisted in dev mode and vanishes in production?
EDIT 1
Using Session
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider()
.AddJsonOptions(config =>
{
config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSession();
.....
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
jquery visual-studio-2017 asp.net-core-mvc tempdata asp.net-core-mvc-2.1
Web Application ASP MVC Core 2.1
I have two models with a Parent-Children relationship, they are Course and CourseEdition; a Course can have many Editions.
The page where I display Course details also shows a list of its editions.
I use JQuery to populate the same page with the details of the edition: when clicking on one of the editions I have a DIV filled with its details data.
There are actions (i.e. enroll one or more person in an edition) that I would like to end with a redirect to the Editions details.
I have set up a mechanism like this:
In the controller action "RegisterPeople" I set TempData with the ID of the Edition I want to be redirected to, then I redirect to the Details action in CourseController.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterEmployees([Bind("CourseId,CourseEditionId,Subscribers")] RegistrationData regData)
{
TempData["NextCourseId"] = regData.CourseId;
TempData["NextEditionId"] = regData.CourseEditionId;
if (ModelState.IsValid)
{
<do something>
return RedirectToAction("Details", "Courses", new { id = regData.CourseId });
}
Here (Course Controller, Details Action) I read TempData and copy its value in ViewBag.
public async Task<IActionResult> Details(int? id)
{
<do something>
ViewBag.GotoEditionId = TempData["NextEditionId"] ;
return View();
}
In the details view I have a DIV with a data-attribute populated by the ViewBag value (the Edition's ID),
<div id="GotoEditionId" data-ceid="@ViewBag.GotoEditionId"> </div>
@section Scripts {
@{await Html.RenderPartialAsync("_CourseScripts");}
}
Now, in _CourseScript.cshtml a JQuery event handler is triggered when clicking on this div does the magic of populating the edition div.
$(document).ready(function () {
$(document).on("click", "#GotoEditionId", function (e) {
e.preventDefault();
var params = {};
params["id"] = $(this).data("ceid");
$.ajax({
url: $("#GetEditionUrl").data('get-edition'),
type: "GET",
data: params,
success: function (result) {
$("#EditionBlock").html(result);
},
error: function (err) {
alert("error");
$("#EditionBlock").html(err.responseText);
}
});
});
var GotoEditionId = $("#GotoEditionId").data("ceid");
if (GotoEditionId > 0) {
$("#GotoEditionId").trigger("click");
}
})
This works perfectly while I am in development with Visual Studio.
When I deploy to a remote IIS server, TempData is filled in the "register" action but it is found empty when entering in the "Details" action.
Who is the culprit? Why TempData is persisted in dev mode and vanishes in production?
EDIT 1
Using Session
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider()
.AddJsonOptions(config =>
{
config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSession();
.....
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
jquery visual-studio-2017 asp.net-core-mvc tempdata asp.net-core-mvc-2.1
jquery visual-studio-2017 asp.net-core-mvc tempdata asp.net-core-mvc-2.1
edited Nov 22 '18 at 8:25
kranz
asked Nov 22 '18 at 1:12
kranzkranz
314319
314319
1
What is yourStartup.cs
? Have you acceptConsentCookie
while accessing the published site? ForTempData
, it usesCookies
orSessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and addTempData["Test"]
to HomeController and access it fromAbout
action.
– Tao Zhou
Nov 22 '18 at 5:44
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
1
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51
add a comment |
1
What is yourStartup.cs
? Have you acceptConsentCookie
while accessing the published site? ForTempData
, it usesCookies
orSessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and addTempData["Test"]
to HomeController and access it fromAbout
action.
– Tao Zhou
Nov 22 '18 at 5:44
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
1
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51
1
1
What is your
Startup.cs
? Have you accept ConsentCookie
while accessing the published site? For TempData
, it uses Cookies
or Sessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and add TempData["Test"]
to HomeController and access it from About
action.– Tao Zhou
Nov 22 '18 at 5:44
What is your
Startup.cs
? Have you accept ConsentCookie
while accessing the published site? For TempData
, it uses Cookies
or Sessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and add TempData["Test"]
to HomeController and access it from About
action.– Tao Zhou
Nov 22 '18 at 5:44
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
1
1
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422606%2fpassing-tempdata-with-redirecttoaction-works-in-visual-studio-but-not-in-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422606%2fpassing-tempdata-with-redirecttoaction-works-in-visual-studio-but-not-in-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is your
Startup.cs
? Have you acceptConsentCookie
while accessing the published site? ForTempData
, it usesCookies
orSessions
. Check whether there is any cookies created while accessing the site. Try to deploy to your development iis to check whether this issue still happens. To check whether it is specific to your project, make a test with built-in asp.net core and addTempData["Test"]
to HomeController and access it fromAbout
action.– Tao Zhou
Nov 22 '18 at 5:44
Added Startup relevant code. On Localhost it does work, on remote IIS it doesnt. Now I'll try a clean project with HomeController as suggested
– kranz
Nov 22 '18 at 8:27
1
Tried with HomeController, default TempData with Cookie works both on localhost and on server. When deploying test application to IIS I have restarted IIS Server. Now all works correctly for my app too (using session). The good old joke about IT people attitude to "shutdown and restart" is confirmed one more time ;o)
– kranz
Nov 22 '18 at 8:59
glad to hear it works now.
– Tao Zhou
Nov 22 '18 at 9:07
Thanks, your suggestion invited me to look into another direction, and probably there was some problem with the IIS server which has disappeared by recycling the server.
– kranz
Nov 22 '18 at 9:51