ASP.NET MVC routing - part of controller name in url - asp.net

I need a routing that maps
admin/{PartOfControllerName}/{action}
to the controller:
Admin{PartOfControllerName}Controller
For example map admin/post/new to AdminPostController and its New action.
Thanks.

one way to do it is to decorate action with Route metadata
[Route("admin/post/new")]
public ActionResult New(...)

Related

Jersey #BeanParam, #HeaderParam alternatives in Spring mvc

In Jersey we can inject a list of headers to a model class using #BeanParam and #HeaderParam. Are there any alternatives in Spring MVC to do the same thing?
I know in spring-MVC, we can inject all headers to a map **#RequestHeader Map<String, Object> headers** and extract the fields from there. But I was wondering if I can inject my required headers into a model class.
Thanks in advance.
Replacement for #HeaderParam is Spring MVC's #RequestHeader
The #RequestHeader annotation allows a method parameter to be bound to a request header.
#RequestMapping
public void displayHeaderInfo(#RequestHeader("Accept-Encoding") String encoding,
And #BeanParam you don't need special annotation, just add your bean to method as is:
#RequestMapping
public String displayHeaderInfo (MyBean myBean) {

How to use "RestController" and "Controller" in an application

1) Using Controller at SingleFileUploadController, gives correct result in jsp and when used RestController instead of Controller in SingleFileUploadController, it is not directing to jsp. Why?
2) Is it possible to use both at same time?
reference:
http://memorynotfound.com/spring-mvc-file-upload-example-validator/
Thanks
Harshal
Because RestController is for controllers who don't forward to views. Their return value is sent as the response body.
Yes, it's possible to have Controllers and RestControllers in the same webapp. If you want some methods of your controller to return views, and some others to return response bodies (i.e. act as in a RestController), then use #Controller, and annotate your "REST" methods with #ResponseBody.
To answer the question in regards to #Controller and #RestController being together.
First controller:
#RestController //specify that this class is a restful controller
#RequestMapping("/api")
public class RestHomeController {
Second Controller
#Controller //specify that this class is a controller
#RequestMapping("/")
public class HomeController {
#Controller tells the api to return ModelAndView Object which contains the name of your view hence the jsp file to view, while #RestController returns serialized response.
No you cannot have them both, controller is either annotated with one of them but as #JB Nizet mentioned you can use #Controller and #ResponseBody to achieve the functionality for #RestController for specific API , anyway this was the trend used since the support for RestController was not there before spring 4.

Adding attribute route breaks config-based route (.NET MVC5)

So, we're updating a project from web forms to .NET MVC. To support other applications that deep link into our application, I'm trying to add attribute routes to the relevant controller actions that mimic the old web forms paths.
I have an event action on the Home controller. The configuration has a route for this to remove the controller name.
routes.MapRoute(
name: "eventdetails_nohome",
url: "event/{id}/{occurrenceid}",
defaults: new { Controller = "Home", action = "Event", occurrenceid = UrlParameter.Optional },
constraints: new { id = #"\d+", occurrenceid = #"\d+" }
);
That route works just fine for routes like http://myapp/event/123/456, and the default routing like http://myapp/home/event?id=123&occurrenceid=456 also works.
So far so good, but if I add this route attribute to the action:
[Route("~/ViewEvent.aspx")]
public ActionResult Event(int id, int occurrenceid)
Then the only route that works is http://myapp/ViewEvent.aspx?id=91918&occurrenceid=165045. The routes that worked before start returning
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: /event/123/456
I've used the routedebugger extension, and I can verify that even with the attribute route, my old route is still the first to work. So why would I be getting "resource cannot be found" errors?
Note: as a workaround, I've found that I can just do a traditional route configuration like
routes.MapRoute(
name: "Legacy event",
url: "ViewEvent.aspx",
defaults: new { Controller = "Home", action = "Event" }
);
I'm still curious why the attribute route would break existing routes, though, as I thought you were supposed to be able to use both at the same time.
Take a look at Attribute Routing in ASP.NET MVC 5
Another article with the same heading
Attribute Routing in ASP.NET MVC 5
Attribute routes overrides the convention based route. If you use more than one URL for action, you can use multiple route attributes on the action...
[Route("event/{id:int}/{occurrenceid:int}")]
[Route("event")]
[Route("~/ViewEvent.aspx")]
public ActionResult Event(int id = 0, int occurrenceid = 0) {
return View();
}
The following URLs all routed to the above action.
http://myapp/event/123/456
http://myapp/home/event?id=123&occurrenceid=456
http://myapp/ViewEvent.aspx?id=91918&occurrenceid=165045

Dynamic Routing with Web API

I have a WebAPI controller with a Get method as follows:
public class MyController : ApiController
{
public ActionResult Get(string id)
{
//do some stuff
}
}
The challenge is that I am attempting to implement WebDAV using Web API. What this means is that as a user browses down a folder structure the URL will change to something like:
/api/MyController/ParentFolder1/ChildFolder1/item1.txt
Is there a way to route that action to MyController.Get and extract out the path so that I get:
ParentFolder1/ChildFolder1/item1.txt
Thanks!
"Dynamic" route is not a problem. Simply use wildcard:
config.Routes.MapHttpRoute(
name: "NavApi",
routeTemplate: "api/my/{*id}",
defaults: new { controller = "my" }
);
This route should be added before default one.
Problem is that you want to end URL with file extension. It will be interpreted as static request to .txt file.
In IIS7+ you can work around that by adding line in web.config:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
Don't forget that if you use MyController, then route segment is just "my"
Use the NuGet package "AttributeRouting Web API". You can specify specific routes for each action, including dynamic parameters.
I was just dealing with this so try it out, and come back if you need more help.

Spring WebFlow: POST from flow to MVC Controller

I have MVC Controller as below and mapped /home to that controller. To redirect to /home from flow i use externalRedirect:contextRelative:/home in view attribute. Is possible to pass some data to /home in POST ?
MVC Controller
#Controller
public class MainController {
#RequestMapping(value="/home", method=RequestMethod.POST)
public String index(#RequestParam String data) {
return "index";
}
}
Flow
<end-state id="home" view="externalRedirect:contextRelative:/home" />
No.
When you are specifying externalRedirect: Spring Webflow is going to set a redirect code and Location header on your response which simply instructs the browser to perform a GET request for the specified location. You can include query parameters appended to this location but not POST data.
For example:
<end-state id="home" view="externalRedirect:contextRelative:/home?foo=bar" />
Also note that you can include ${expressions} in this string that will be evaluated against the request context, according to the XSD.

Resources