I have an asp.net mvc 4 application. There is an action method, HttpPost which takes in a formcollection. I want to provide a direct link to this action and manually set a parameter on the formcollection. Is this possible?
e.g. myurl?formCollection[Parameter]=staticValue
You can't provide a direct link:
Direct link
because links use GET's (not POST's). Though you can get the behavior that you want using some javascript or changing the action to HttpGet.
Regards.
Related
I can't use #Url.Action in ASP.NET Core because I have error:
IUrlHelper has no applicable method named Action
I want to use #Url.Action for:
page i
In ASP.NET MVC it generated me urls for example:
/Home/List?page=5&search=abc
In ViewBag.routeValues I pass additional parameters, for example: search=abc&active=true and paste it to url after 'page' parameter.
I think I can't do the same using tag helper and 'asp-route' - so what can I do?
Ok - I need to cas ViewBag to string:
page i
You should first read the ASP.NET Core Documentation to find out what tag helpers are and how to use them.
In ASP.NET Core's Razor implementation, tag helpers are used instead of helper methods.
page i
would translate into
<a asp-controller="#ViewBag.actionName" asp-action="#ViewBag.controllerName" asp-route-id = "#i">page i</a>
"asp-route" can be either a route parameter or a query parameter (if the given action has no such route parameter). so asp-route-xyz="abc" would add ?xyz=abc to the url.
In my MVC application, I dont want any user to type in the address bar of the browser and navigate to any controller action directly.Can I enable this for the whole application?if yes ,How? Can you please let me know the concept name ?
Also I dont want to do that through Request.URLReferrer because it has its own security risks (per Avoiding user to navigate to a view by entering url in the browser)
Thanks in advance!
You need to use Custom Action Filter Attributes, See :
http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-custom-action-filters
****Updated:**
As Parsanna mentioned in comment,
You can use the [ChildActionOnly] attribute on your action method to make sure it's not called directly, or use the ControllerContext.IsChildAction property inside your action to determine if you want to redirect.
See :Asp.net mvc How to prevent browser from calling an action method?
I am creating a dnn module. The content depends on the param in the url.
I want to be able to edit this content in the 'edit content' mode. However when i go to edit content the param in the url is no longer accessible because it is the parent document. How do i go about passing this value from the view.ascx to the edit.ascx?
Try storing the param in cookies or localstorage. Then you should be able to access it. Of course the user will be able to modify it but you can do a check that the user has not modified it by storing a server side encryption or somthing like that.
A workaround is to have a field where the user enters this parameter. But i know this isn't a very good solution. I am guessing that you will have to override the dotnetnuke core to do this (yes i know it sucks).
I hope I am understanding the question properly.
To pass parameters from your View to Edit controls, you should first make sure they are registered properly in the module definition. You default View should have an empty controlkey and your Edit should be registered with a control key, for example "addedit".
When creating a link between your view control and edit control, use the EditUrl() method of PortalModuleBase. When passing a parameter, for example an id of the item you want to load into your edit control, you can pass them as arguments in the EditUr method.
Example (in my view.ascx.cs):
lnkEdit.NavigateUrl = EditUrl("id", "16", "addedit");
This will assign a module view link to the edit.ascx (assuming the controlkey in the definition is addedit) passing in a url parameter "id" with value 16.
See my DNN Module Views tutorial for a complete lesson on how to do DNN module views and navigation.
http://www.dnnhero.com/Premium/Tutorial/tabid/259/ArticleID/204/Introduction-and-Module-Definition-basics-in-DNN-Part-1-6.aspx
I have a method in my controller that I don't want to be called from the address bar in the browser...
Is there a way to do that? Maybe some kind of annotation, modification in the route configuration? Which are my options?
If you are going to use this action only from within your controller or Views then you can use ChildActionOnly attribute.
If you want to access it using POST then you can use [HttpPost] attribute.
But if you wish to use it using GET (i.e. using AJAX call etc) and don't want users to access it using address bar then you can follow this tutorial to make your actions AJAX only.
Or, if you simply want a method that is not an Action at all (i.e. cannot be called using HTTP) then you can either make it private or use [NonAction] attribute
Use NonAction attribute on the method.
I am trying to do something very simple and I seem to be missing something. I tried to scour the internet for results but haven't gotten anywhere so I was wondering if someone can please advise on this seemingly easy and straightforward task.
I have a working MVC Application and have created Models, Controllers, Views using the defaults (scaffolding).
Now I want to create a new view for one of my controller actions:
public ActionResult Index()
{
return View(db.Blog.ToList());
}
So I right click on Action Result and click Add View.
This gives me a dialog box where I specify a view name of "Test", I click "Create a Strongly Typed View" check box and select model class of Blog.
For scaffold template, I leave empty (note I have tried index without any good result)
Now I click the Add button.
As expected this creates a new view test.cshtml under Views/Blogs
Now when I begin without debugging and go to url: localhost:12341/Blog/Test
I get the following error:
Server Error in '/' Application.
The resource cannot be found.
Description: 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.
Requested URL: /Blog/test
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
There could be a lot of issues with why it doesn't work. It is probably worth your while to look into ASP.NET MVC routing. For now, Mystere Man's solution might be a "quick fix" assuming you have everything else set up to their defaults.
In particular, when you tell your browser to go to localhost:12341/Blog/Test then it will (probably) look for a Controller called BlogController and then perform the Test action. In your case, your action is called Index so you would want to go to localhost:12341/Blog/Index (though you may be able to omit Index since it's the default action). Lastly, since your action is called Index then the View() function will automatically look for Index.cshtml. This is detailed somewhat in the msdn documentation:
http://msdn.microsoft.com/en-us/library/dd492930(v=vs.100).aspx
In particular:
If the ViewName property is empty, the current action name is used in place of the ViewName property.
Your action method is called Index, not Test. If you want the url to be /Test, then you need to name the action method Test (there are other ways to do it, but this is the best way)
If you want to use the view Test, then you need to specify it in your View() method.
return View("Test", db.Blogs.ToList());
However, you will still need to use the Index url /Blogs/Index because your action method is Index.