How Can I Call To aspx page in Controller Class in asp.net
Loading
How Can I Call To aspx page in Controller Class in asp.net
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Deepak RawatPosted May 19, 2023, 5:06 AM
To call an ASPX page from a controller class in ASP.NET, you can use the
Server.Transfer()method. This method transfers control from the current page or handler to the specified ASPX page while maintaining the original URL in the browserhis example, the
CallAspxPage()method in your controller class is called when a specific action is triggered. You can add your own processing or business logic before calling the ASPX page.The
Server.Transfer("~/YourPage.aspx")statement transfers control to the specified ASPX page, which is represented by the tilde (~) and the relative path to your ASPX page. You need to provide the correct path to your ASPX page.After calling
Server.Transfer(), any code following this statement in the controller method will not be executed since control is transferred to the ASPX page. If you need to execute additional code or redirect after the transfer, you can place it before theServer.Transfer()statement.Vishal JoshiPosted May 18, 2023, 12:24 PM
Hello,
Use the Controller's Redirect() method. you need to pass the full path with the domain name to redirect or call the aspx page from controller. checkout below sample code.
Thanks
Vishal
Jagapathibabu BanothPosted May 17, 2023, 12:32 PM
>Not Working Sir......
Rajkiran SwainPosted May 17, 2023, 4:58 AM
To call an ASPX page from a controller class in ASP.NET, you can use the `Redirect` or `RedirectToAction` method. Here's how you can do it:
1. In your controller class, import the `System.Web.Mvc` namespace.
using System.Web.Mvc;
2. Create an action method in your controller that will handle the request to navigate to the ASPX page.
public ActionResult RedirectToAspxPage()
{
return Redirect("~/Path/To/YourPage.aspx");
}
Replace `"~/Path/To/YourPage.aspx"` with the actual path to your ASPX page.
3. Alternatively, if you have a separate action method for the ASPX page, you can use the `RedirectToAction` method.
public ActionResult RedirectToAspxPage()
{
return RedirectToAction("YourActionMethod", "YourController");
}
Replace `"YourActionMethod"` and `"YourController"` with the actual names of your action method and controller that handle the ASPX page.
4. You can also pass parameters to the ASPX page by including them in the redirect URL.
public ActionResult RedirectToAspxPageWithParams()
{
string param1 = "value1";
string param2 = "value2";
return Redirect($"~/Path/To/YourPage.aspx?param1={param1}¶m2={param2}");
}
In this example, the values of `param1` and `param2` will be passed as query parameters to the ASPX page.
Make sure to replace `"~/Path/To/YourPage.aspx"` with the correct path to your ASPX page, and modify the action method and controller names as needed.