i am creating a partialview with a controller action like:
public ActionResult GetPostsByUser(string userName)
{
where userName is part of the URL:
www.example.com/User/toddM
toddM being the userName
First off.. am i going about this the right way?? if i make it a querystring ?userName=toddM it works.. but i need it to read from the URL. Again, this is a partialview . thanks!
Your Url sample is not "well formed" as it miss one of the controller/action.
In fact, as per the default route created by the MVC project template you should have
"{controller}/{action}/{id}", // URL with parameters
An URL like www.example.com/Post/User/toddM would perfectly fits within the default route and so I think will work without any problem.
This one would be your action in an hypothetical PostController
public ActionResult User( string id )
{
//id will contain toddM
}
Related
I'm very confused about the design of my RESTful services!
If I was doing this using vanilla MVC3/4 then I would simply have action methods marked [HTTPGet] etc. and I could have multiple Get's per controller. I this way I would organise controllers by their "meta group".
I've looked at the Web API MVC4 template and it gives me the automatic translation from an Http GET to the Getxxx() method - but this implies a single Get per controller and organising controllers by object, rather than function...which seems to make some sense.
I see many posts on adding named routes - but this seems to break the natural model of Get, Post, Put, Delete. If I do that - then aren't I (in essence) just going back to vanilla MVC4?
Is there any impact on having lots of controllers?
Am I thinking
about this correctly?
Shortly,
Is there any impact on having lots of controllers?
No
Am I thinking about this correctly?
Generally yes.
Default WebAPI/MVc template uses routing that relays on prefixes and naming GetXXX, PostXX.
RouteTable.Routes.MapRoute(
"WithActionApi",
"api/{controller}/{action}/{id}"
);
But you can create your own custom routing with action names instead. Then you uses in URL name of your action method and as you've wrote Attributes to set HTTP Verbs like [HttpGet]
RouteTable.Routes.MapRoute(
"DefaultApi",
"api/{controller}/{id}",
new { action="DefaultAction", id = System.Web.Http.RouteParameter.Optional }
);
[ActionName("DefaultAction")] //Map Action and you can name your method with any text
public string Get(int id)
{
return "object of id id";
}
[HttpGet]
public IEnumerable<string> ByCategoryId(int id)
{
return new string[] { "byCategory1", "byCategory2" };
}
I am looking at using WebAPI to create a Restful API. I want to create a launch url to provide URLs to the other portions/entities of the API. The obvious place to do this is ~/api/ however I cannot seem to wire up a route that will not give a 403 result. Any ideas?
EDIT: Just to be a bit clearer. I know how the default routing works in WebAPI, I.E. if I create a CustomersController that inherits from ApiController I can get there using ~/api/customers. What I want is a step before that where I could go to ~/api/ and would get a result a bit like:
[
{ Title: 'Customers', Url: '~/api/customers' }
]
I want this as my understanding is that RESTful services are metadata (think that the term) driven (basically discoverable and provide links to other resources in result). So there should be a single source url that points to all other resources in the API.
All you need to do for this is to add a new route which handles ~/api specifically.
In your project you will notice in the Application_Start method in the Global.asax a call is made to WebApiConfig.Register(GlobalConfiguration.Configuration), the WebApiConfig class is included in your project in the App_Start folder (along with various other config classes). If you look at the implementation of the Register call you will see that's where the ~/api/controller route is actually setup i.e.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In order to make ~/api discoverable all you need to do is introduce a new route before the default which handles any calls to ~/api e.g.
config.Routes.MapHttpRoute(
name: "DiscoverableApi",
routeTemplate: "api",
defaults: new { controller = "Discoverable", }
);
Then add an ApiController to handle that call e.g.
public class DiscoverableController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "/users", "/photos", "/history" };
}
}
If you don't want to hard-code your URLs you will most likely need to look at something like Reflection to enumerable all the available ApiController's and their reachable endpoints (i.e. actions).
Take a look at the Microsoft ASP.NET Web API Help Page package from NuGet. Out of the box you can create a list of all your API endpoints which are returned as an MVC View. However you could customise it to return Json instead. You can also configure it to hang off any route you want, i.e.
api.mysite.com/help
myapi.com/api
etc
I have added an Area to my Web project wherein I hope to perform all the necessary authentication tasks. The folder structure in the project is:
- Solution
- Web Project
- Areas
- Accounts
- Controllers
AccountController.cs
In the AccountController is the usual LogOn(LogOnModel model, string returnUrl) Action.
In the AccountsAreaRegistration.cs is the auto-generated route:
context.MapRoute(
"Accounts_default",
"Accounts/{controller}/{action}/{id}",
new {
action = "LogOn",
id= UrlParameter.Optional
}
);
The first thing that occurred to me was to simply add the name of the area to the loginUrl attribute in the web.config, and let the route mapping take care of the redirect - pretty simple, I thought:
<forms loginUrl="~/Accounts/Account/LogOn"
timeout="2880" />
The only problem is that it doesn't work. I get an error saying "unable to find "/Accounts/Account/Logon" - or whatever value I put in the loginUrl attribute. It seems to me that the URL I specify in the Web.Config isn't pushing that value through the route table to look for a match.
*note: all of this is being triggered by an [Authorize] attribute on an action in one of my other controllers.
EDIT
The workaround I have found is to leave the URL as the default ("Account/Logon") and add another route in the global.asax to redirect the request to the right area:
routes.MapRoute("LogOn",
"Account/LogOn/{id}",
new { controller = "Account",
action = "LogOn",
id = UrlParameter.Optional}
).DataTokens.Add("area", "Accounts");
This gets the job done, but I don't know if it's the best solution to the problem.
Is the controller action you're testing tagged with the [Authorize] attribute?
Which part isn't working, the authorization of some action or the routing to the logon action after requesting an authorized action?
The workaround I have found is to leave the URL as the default ("Account/Logon") and add another route in the global.asax to redirect the request to the right area:
routes.MapRoute("LogOn",
"Account/LogOn/{id}",
new { controller = "Account",
action = "LogOn",
id = UrlParameter.Optional}
).DataTokens.Add("area", "Accounts");
This gets the job done, but I don't know if it's the best solution to the problem.
I need to create a url scheme like this
friend/{userid}/wishlist
where friend is the controller, wishlist is the view, and userid is the id of hte friend whose wishlist you would like to see.
I have setup a route like this
routes.MapRoute(
"FriendWishlist",
"friend/{userid}/wishlist",
new { controller = "WishList", action="FriendWishlist", userid = 123}
);
when i try to browse to /friend/123/wishlist i get the following error
A public action method '123' was not
found on controller
'GiffrWeb.Areas.Api.Controllers.FriendController'.
Routes in MVC are evaluated in the order they are declared. It sounds very much like you have declared your route below the default one:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"FriendWishlist",
"friend/{userid}/wishlist",
new { controller = "WishList", action="FriendWishlist", userid = 123}
);
So the MVC framework is trying to match your URL /friend/123/wishlist first to the default route. Because it's all variables and everything has a default or is optional, it's guaranteed to match. It doesn't check if the controllers and actions exist and take the relevant arguments. You have a FriendController class - check. 123 action - it goes bang.
Simplest fix - declare the route above the default one (ie just swap these two statements) and it should work OK.
I might just add that it seems a little weird to have a URL that starts with /friend/ going to a WishList controller when you obviously have a Friend controller (your error message says so).
Finally, I can't recommend highly enough that if you introduce custom routing that you also test those routes thoroughly - as you have seen, the routing engine often might not do what you think it does. I recommend either the route testing stuff in MvcContrib or Brad Wilson's blog post.
http:www.site1.com/?sid=555
I want to be able to have the sid parameter and value persist whether a form is posted or a link is clicked.
If the user navigates to a view that implements paging, then the other parameters in the querystring should be added after the sid.
http:www.site1.com/?sid=555&page=3
How can I accomplish this in Asp.Net Mvc 2?
[Edit]
The url I mentioned on top would be the entry point of the application, so the sid will be included in the link.
Within the application links like:
<%= Html.ActionLink("Detail", "Detail", new { controller = "User",
id = item.UserId })%>
should go to:
http:www.site1.com/user/detail/3?sid=555
This question is different than what Dave mentions, as the querystring parameter is persisting throughout the site.
Firstly, I'd say if the value needs to be persisted throughout the session then you should store it in Session and check that its still valid on each action call. This can be done through a custom action attribute you add to the controller / actions required. If the value is required then when the value is checked you can re-drect to a login page or similar if not present or its expired.
Anyway, that said I thought I would have a crack at getting it working. My first thought would be to create a custom action filter attribute which took the value of the querstring and stored it in session in OnActionExecuting and then OnResultExecuted would add the key back to the querystring. But as QueryString in Request is a read-only collection you can't do it directly.
So, whats now available to you?
Option #1 - Add it to all calls to Html.ActionLink() manually
or ...
Option #2 - Override a version of ActionLink which automatically adds the value for you. This can be achived like so. I wouldn't recommend doing this though.
Start off with the custom attribute.
public class PersistQueryStringAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var sid = filterContext.RequestContext.HttpContext.Request.QueryString["sid"];
if (!string.IsNullOrEmpty(sid))
{
filterContext.RequestContext.HttpContext.Session["sid"] = sid;
}
base.OnActionExecuting(filterContext);
}
}
All this does is check the request querystring for the required key and if its available add it into the session.
Then you override ActionLink extention method to one of your own which adds the value in.
public static class HtmlHelperExtensions
{
public static MvcHtmlString ActionLink<TModel>(this HtmlHelper<TModel> helper, string text, string action, string controller, object routeValues)
{
var routeValueDictionary = new RouteValueDictionary(routeValues);
if (helper.ViewContext.RequestContext.HttpContext.Session["sid"] != null)
{
routeValueDictionary.Add("sid", helper.ViewContext.RequestContext.HttpContext.Session["sid"]);
}
return helper.ActionLink(text, action, controller, routeValueDictionary, null);
}
}
On each of the action which is going to be called apply the attribute (or apply it to the controller), eg:
[PersistQueryString]
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
Note
As the query value gets put into session it will be applied for the life of the session. If you want to check that the value is there and the same each request you will need to do some checking in the attribute overridden method.
Finally
I've purely done this as a "can it be done" exercise. I would highly recommend against it.
Possible Duplicate:
How do you persist querystring values in asp.net mvc?
I agree with the accepted answer to the question linked above. Querystring parameters are not designed for data persistence. If a setting (i.e. sid=555) is intended to persist through a session, use Session state or your Model to save that data for use across requests.