ASP .NET MVC - getting null querystring values in controller - asp.net-mvc

I'm trying to reach a controller method by providing some url parameters :
http://localhost/GoTo/Index?topic=test
Here's the method I want to reach :
public ActionResult Index(string topic)
{
//do some stuff
}
Problem is, the topic parameter in the method is always null. I also tried to check what I got in Request.QueryString collection, but it's empty.
Any lead/explanation would be appreciated.
EDIT : Screenshot requested :

Please try this it's working for me check below screenshot
Just call you're URL like this
http://localhost/GoTo?topic=test
public class TestingController : Controller
{
// GET: Testing
public ActionResult Index(string topic)
{
return View();
}
}

Related

MVC 5 Web api 2 Attribute Routing not working for api/v1/portfolio/1/credentials

Code:
[RoutePrefix("api/v1/portfolio")]
public class PortfolioController : ApiController
{
[Route("")]
[Route("index")]
public IEnumerable<PortfolioModel> GetPortfolios()
{
...
}
[Route("{id:int}")]
[ResponseType(typeof(PortfolioModel))]
public IHttpActionResult GetPortfolio(int id)
{
....
}
[Route("{id:int}/credentials")]
[ResponseType(typeof(IEnumerable<CredentialsModel>))]
private IEnumerable<CredentialsModel> GetCredentialsForId(int id)
{
....
}
}
api/v1/portfolio works
api/v1/portfolio/index works
api/v1/portfolio/1 works
api/v1/portfolio/1/credetials DOES NOT WORK
getting:
HTTP Error 404.0 - Not Found. The resource you are looking for has been removed
Can any of you gurus tell me what I'm doing wrong?
In case you ask, my WebApiConfig only has
config.MapHttpAttributeRoutes();
and my RegisterRoutes has only:
routes.MapMvcAttributeRoutes();
as always, thank you for the help.
change it from private to public
private IEnumerable GetCredentialsForId(int id)
to
public IEnumerable GetCredentialsForId(int id)
it will work!
I finally solved the problem. I'll explain for others having similar problem:
As you see in the previous code, I have the action (returning credentials) in the portfolio controler, instead I use similar action in my Credentials controler and map routed the action as if coming from the portfolio controler.
[Route("~/api/v1/portfolio/{id}/credentials")]
[ResponseType(typeof(IEnumerable<CredentialsModel>))]
public IEnumerable<CredentialsModel> GetByPortfolioId(int id)
{
return _repository.FindByPortfolioId(id);
}
so, I have to map to the controler in charge of returning a particular model. My problem was I had an action returning CredentialModel in a controler in charge of handling PortfolioModel.

By default not redirecting to Index action method of controller

Recently I've created on controller call DashboardVideos and an action method called Index.
And after Add Or Update, I'm redirecting it to Index page using
RedirectToAction("Index", "DashboardVideos").
but this code redirecting it to /DashboardVideos/ and it says
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
so the issue is by default it's supposed to load Index page when I say /Dashboard
But its not, same url pattern working with all other controller (So I don't think there's anything wrong with routing pattern).
Any help would be appreciated.
Code:
public class DashboardVideosController : BaseController
{
private readonly IDashboardVideosComponent socialTagComponent;
public DashboardVideosController()
{
socialTagComponent = ComponentFactory.Get<IDashboardVideosComponent>();
}
// GET: DashboardVideos
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index", "DashboardVideos");
}
}
Simply write this if both actions are in same controller.
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index");
}
Try to take a look at your "RouteConfig" class and you can specify custom routes there. Also It is possible if the call comes from AJAX it can go directly to action without redirecting. Did you tried to Debug the code?

MVC: trying to understand where ActionResult sends it's data

I have been trying to understand an online tutorial and I am stumped.
Can someone please tell me where the text "Hello" is sent to? Is the message sent directly to the browser without being placed on a page?
public class GoHomeController : Controller
{
public string Index()
{
return "Hello";
}
}
How's this? Your controller action needs to have a return type of ActionResult, there are many subclasses of this class that allow for various types of responses however you can always influence with brute force if you like. For example"
public ActionResult Index()
{
Response.Write("hello world");
return null;
}
The above code writes to the Response stream directly, in my example I return a null. This indicates no ActionResult is needed to be performed by the MVC system, typically this is where the View is specified, the View will be read, parsed and written to the Response stream as well.
But typical controller actions do have return values, for example here is how I could return JSON, remember the View is just an abstraction to allow you to control what is written to the Response stream.
public ActionResult Index()
{
return Json( new { Message="Hello world"});
}
And then there is the typical ActionResult that directs the output to a .cshtml file:
public ActionResult Index()
{
return View();
}
This will write to the Response stream using the Index.cshtml file tied to this controller namespace or I could specify the name of the .cshtml:
public ActionResult Index()
{
return View("HelloWorld"); //<-- looks for HelloWorld.cshtml
}

Difference between Private methods and Actions decorated with NonAction in Asp.Net MVC

in Asp.Net MVC if I decorate an action method with attribute NonAction then it wont be allowed to be called by the user visiting the site.
same happens when I make it private
So whats the difference between the two and is there a special purpose for which NonAction attribute has been made?
For example whats the difference between
[NonAction]
public ActionResult SomeAction(){}
And
private ActionResult SomeAction(){}
in the context of asp.net MVC of course I know one is public and the other one is private
That's the only difference. The attribute is used when you want a method that has a signature that would make it an action, but that you don't want to be an action.
An example for a use for that is a method that action methods call to produce the ActionResult for them:
[NonAction]
public JsonResult JsonInfo(string id, string value) {
return Json(new { id = id, value = value });
}
public JsonResult GetBusInfo() {
return JsonInfo("4", "Bus");
}
public JsonResult GetCarInfo() {
return JsonInfo("8", "Car");
}
The reason to make it public instead of private would be so that actions in other controllers could also use it.
Both works same with action method,you can use them seperately or together.
[NonAction]
private ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
FEED_TBL fEED_TBL = db.FEED_TBL.Find(id);
if (fEED_TBL == null)
{
return HttpNotFound();
}
return View(fEED_TBL);
}
If declare it like the above code then when we will try to go to details action method it will not go to it.Rather it will show the error.
{{ HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.}}
This shows that our detail link on view does found any reference to details action method and our controller to.

MVC 4 Routing , Get/Post requests handling

I'm faced with the following problem :
I have a controller with lets say the following actions:
[HttpGet]
public ActionResult Index()
{
var viewModel = new IndexViewModel();
return View("Index", viewModel);
}
[HttpPost]
public void ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
}
The problem is the following:
The User enters on Index page with this URL : /Controller/Index
Then the user submits the form to Action ExportToExcel
Data is exported to Excel( file downloaded ) and it's okay.
The URL becomes /Controller/ExportToExcell
Then when I am clicking "Enter" I am going To /Controller/ExportToExcell but with GET
and of course falling with Page Not Found, the question is how properly to Deal with this in MVC
Don't use void as returned type of your post action, use an ActionResult
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
return RedirectToAction("Index");
}
I believe that your problem is that you aren't returning a FileResult, and the browser will redirect you to your post path. Can't test it right now, but I believe the following should work.
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Generate the Excel file into a MemoryStream for example
// Return a FileResult with the Excel mime type
return File(fileStream, "application/vnd.ms-excel", "MyExcelFile.xls");
}
Check FileResult and Controller.File for more details.
As a note, I'm not completely sure if that's the mime type for an Excel file, but if you say you are already downloading the file, your probably already have it :)
You must return ActionResult instead of void.
public ActionResult ExportToExcel(PagingParams args)
{
var data = new StudentDataContext().Student.Take(200).ToList();
return data.GridExportToExcel<Student>("GridExcel.xlsx", ExcelVersion.Excel2007, args.ExportOption);
}
Please check the link: Export Action

Resources