Polymorphism in action methods MVC - asp.net-mvc-3

I have two actions:
//Action 1
public FileResult Download(string folder, string fileName) { ... }
//Action 2
public FileResult Download(int id, string fileName) { ... }
When I try to download the following URL:
http://localhost:54630/Downloads/Download/15?fileName=sharedhostinggsg.pdf
The error happens:
The current request for action 'Download' on controller type 'DownloadsController' is ambiguous between the following action methods:
System.Web.Mvc.FileResult Download(Int32) on type SextaIgreja.Web.Controllers.DownloadsController
System.Web.Mvc.FileResult Download(System.String, System.String) on type SextaIgreja.Web.Controllers.DownloadsController
How can I make them:
Url: ../Downloads/Download/15?fileName=sharedhostinggsg.pdf
Action: Action 2
Url: ../Downloads?folder=Documentos$fileName=xx.docx
Action: Action 1
I tried to put a constraint on my route, but did not work:
routes.MapRoute(
"Download", // Route name
"Downloads/Download/{id}", // URL with parameters
new { controller = "Downloads", action = "Download" }, // Parameter defaults
new { id = #"\d+" }
);
Searching the Internet I found several links but I could not understand how I can solve my problem. This, for example, the RequireRequestValue attribute is not found. I do not know which namespace it is.

The RequireRequestValue that you mention is a custom class they created (from Example posted)so you will not find it in any Microsoft namespace.
The class you will see inherits from ActionMethodSelectorAttribute. This attribute class can be used to help filter actions much like the AcceptVerbs attribute. So as in the example of that link they are returning true or false dependant on if a value is specified in the route arguments.
So following from that example you posted, create a class called RequireRequestValueAttribute. Then decorate your two Downloads action methods like so:
[RequireRequestValue("id")]
public FileResult Download(int id, string fileName) { ... }
[RequireRequestValue("folder")]
public FileResult Download(string folder, string fileName) { ... }

Related

ASP.NET core HttpGet single Web API

Good Morning,
I’m having difficulty setting up my HTTPGETs and then testing the solution in Postman.
I’m trying to return a single result on both occasions however when I input the parameters nothing loads. So I'm clearly missing something which i need some help on please.
I have 1 parameter {id} in my CashMovementController and if I navigate to localhost/api/cashmovements/{id} it loads however if pass the {id} parameter in postman it fails.
Then in my BondCreditRatingsController I have 2 parameters {ISIN} & {Date} and again I'm not sure how to approach this.
Love to hear some advice/help on this please
Thanks GWS
Startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
CashMovementsController.cs
[Route("api/[controller]")]
public class CashMovementsController : Controller
{
private ICashMovementRepository _cashmovementRepository;
[HttpGet("{id}", Name = "GetCashMovement")]
public IActionResult Get(int id)
{
CashMovement _cashmovement = _cashmovementRepository.GetSingle(u => u.CashMovementId == id);
if (_cashmovement != null)
{
CashMovementViewModel _cashmovementVM = Mapper.Map<CashMovement, CashMovementViewModel>(_cashmovement);
return new OkObjectResult(_cashmovementVM);
}
else
{
return NotFound();
}
}
}
BondCreditRatingsController.cs
[Route("api/[controller]")]
public class BondCreditRatingsController : Controller
{
private IBondCreditRatingRepository _bondcreditratingRepository;
public BondCreditRatingsController(IBondCreditRatingRepository bondcreditratingRepository)
{
_bondcreditratingRepository = bondcreditratingRepository;
}
[HttpGet("{id}", Name = "GetBondCreditRating")]
public IActionResult Get(string id, DateTime efffectivedate)
{
BondCreditRating _bondcreditrating = _bondcreditratingRepository.GetSingle(u => u.ISIN == id, u => u.EffectiveDate == efffectivedate);
if (_bondcreditrating != null)
{
BondCreditRatingViewModel _bondcreditratingVM = Mapper.Map<BondCreditRating, BondCreditRatingViewModel>(_bondcreditrating);
return new OkObjectResult(_bondcreditratingVM);
}
else
{
return NotFound();
}
}
If you want to map it to api/Controller/method/id you would need to use the code below because you want to map parameter order (no other identifier) to a specific parameter name in the action.
[HttpGet("GetCashMovement/{id}")]
Your current code should work with below since you are using named parameters and because the request can't be mapped to any other template.
/api/CashMovements/GetCashMovement?id=1
But that attribute syntax will also (possibly unintentionally) trigger:
/api/CashMovements/1
Since a sum of your defined template for that action is:
[Route("api/[controller]/{id}")]
Reason to why /api/ApiTest/GetCashMovement maps GetCashMovement.Get(int i) is because id is defined as optional in startup
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/**{id?}**");
A question mark (?) after the route parameter name defines an optional
parameter.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#create-routes

Asp.Net WebAPI and AttributeRouting - no overloaded attribute constructor?

I have just downloaded AttributeRouting NuGet package for WebAPI and having a problem within my controller.
I thought the way to use it was to have something like:
public class InboxController : ApiController
{
private IInboxService _inboxService;
public InboxController(IInboxService inboxService)
{
_inboxService = inboxService;
}
public IEnumerable<MessageModel> GetAll()
{
return _inboxService.GetAllMessages();
}
[HttpGet("Inbox/Count")]
public int GetInboxCount()
{
return _inboxService.GetMessageCount();
}
}
However I get the following error:
Error 2 'System.Web.Http.HttpGetAttribute' does not contain a constructor that takes 1 arguments
I need to get this up and running fairly quickly. Is there any reason why the HttpGet attribute doesn't have an overloaded constructor?
UPDATE
[GET("Inbox/EnquiryCount")]
public EnquiryCountModel GetEnquiryCounts()
{
var model = new EnquiryCountModel();
model.EnquiryCount = _inboxService.GetCustomerEnquiriesCount();
model.EnquiryResponseCount = _inboxService.GetCustomerEnquiryResponseCount();
return model;
}
In routes:
routes.MapHttpRoute("InboxEnquiryApi", "api/inbox/{action}", new { Controller = "Inbox" }, null, new WebApiAuthenticationHandler(GlobalConfiguration.Configuration));
When I hit the URL at 'api/inbox/EnquiryCount' I get the this error:
**No HTTP resource was found that matches the request URI 'http://localhost:49597/api/inbox/enquirycount'**
Attribute routing is supported in Web api 2
Here is the details: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
This syntax has been changed in newer versions of the webapi. The [HTTPPOST] is now standalone and there is a new attribute for the route aptly name ROUTE which takes the route url
eg.
[Route("GetRes/{month}")]

Sitecore context not loaded in custom controller

I followed this tutorial, and created this code:
using Glass.Sitecore.Mapper;
using Sitecore.Mvc.Controllers;
using Sitecore.SecurityModel;
using SitecoreCMSMVCBase.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SitecoreCMSMVCBase.Controllers
{
public class CommentController : SitecoreController
{
ISitecoreContext _context;
ISitecoreService _master;
public CommentController()
: this(
new SitecoreContext(),
new SitecoreService("master"))
{
}
/// <summary>
/// This constructor can be used with dependency injection or unit testing
/// </summary>
public CommentController(ISitecoreContext context, ISitecoreService master)
{
_context = context;
_master = master;
}
[HttpGet]
public override ActionResult Index()
{
var model = _context.GetCurrentItem<CommentPage>();
return View(model);
}
[HttpPost]
public ActionResult Index(Comment comment)
{
var webModel = _context.GetCurrentItem<CommentPage>();
if (ModelState.IsValid)
{
var masterModel = _master.GetItem<CommentPage>(webModel.Id);
if (masterModel.CommentFolder == null)
{
CommentFolder folder = new CommentFolder();
folder.Name = "Comments";
using (new SecurityDisabler())
{
_context.Create(masterModel, folder);
}
masterModel.CommentFolder = folder;
}
using (new SecurityDisabler())
{
comment.Name = DateTime.Now.ToString("yyyyMMddhhmmss");
//create the comment in the master database
_master.Create(masterModel.CommentFolder, comment);
webModel.CommentAdded = true;
}
}
return View(webModel);
}
}
}
Models are identical with tutorial, so I will not paste them.
My route configuration looks like this:
routes.MapRoute(
"CommentController", // Route name
"Comment/{action}/{id}", // URL with parameters
new { controller = "Comment", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When I navigate to /comment I see this exception:
Glass.Sitecore.Mapper.MapperException: Context has not been loaded
I tried with commenting my route specification (as there was nothing about routes in tutorial), and then error is different (throwing by Sitecore CMS itself):
The requested document was not found
Do you know how to load Sitecore context into custom Controller, and make this simple example work? I was looking everywhere but couldn't find any good answer...
I think this is more a Glass setup issue, rather than an MVC routing problem.
To setup Glass, you need to initialise the context in your application start method in your Global.asax file.
var loader = new Glass.Sitecore.Mapper.Configuration.Attributes.AttributeConfigurationLoader(
"Glass.Sitecore.Mapper.Tutorial.Models, Glass.Sitecore.Mapper.Tutorial");
Glass.Sitecore.Mapper.Context context = new Context(loader);
For other Glass-setup related stuff I recommend following the first tutorial on the glass.lu website.
http://www.glass.lu/tutorials/glass-sitecore-mapper-tutorials/tutorial-1-setup/
This method doesn't need Glass at all!
First step is to set your route in Global.asax file.
routes.MapRoute(
"DemoController", // Route name
"Demo/{action}/{param}", // URL with parameters
new { controller = "Demo", action = "Index", param = "", scItemPath = "/sitecore/content/DemoHomePage" } // Parameter defaults
);
Notice that controller is not taken as parameter, but is fixed, to prevent handling it by Sitecore. More info here and here. Notice that there is one additional parameter - scItemPath. It contains path to item which by default will be included in page context.
Having this route our traffic from /demo is handled by DemoController and Index action. Inside this action all you need is to add is this line:
Sitecore.Data.Items.Item item = Sitecore.Mvc.Presentation.PageContext.Current.Item;
item variable will contain your Sitecore item pointed by scItemPath.
And that's all - it should work well now - hope it helps!

MVC 3 How to tell what view a controller action is being called from-

Is there a way to tell what view a controller action is being called from?
For example, I would like to use "ControllerContext.HttpContext.Request.PhysicalPath" but it returns the path in which the controller action itself is located:
public ActionResult HandleCreateCustomer()
{
// Set up the customer
//..code here to setup the customer
//Check to see of the calling view is the BillingShipping view
if(ControllerContext.HttpContext.Request.PhysicalPath.Equals("~/Order/BillingShipping"))
{
//
return RedirectToAction("OrderReview", "Order", new { id = customerId });
}
else
{
return RedirectToAction("Index", "Home", new { id = customerId });
}
}
If you have a fixed number of locations that it could possibly be called from, you could create an enum where each of the values would correspond to a place where it could have been called from. You'd then just need to pass this enum value into HandleCreateCustomer, and do your condition statement(s) based on that.
At the moment I am using something of the sort:
In the View I am populating a TempData variable using:
#{TempData["ViewPath"] = #Html.ViewVirtualPath()}
The HtmlHelper method ViewVirtualPath() is found in the System.Web.Mvc.Html namespace (as usual) and is as follows and returns a string representing the View's virtual path:
public static string ViewVirtualPath(this HtmlHelper htmlHelper)
{
try{
return ((System.Web.WebPages.WebPageBase)(htmlHelper.ViewDataContainer)).VirtualPath;
}catch(Exception){
return "";
}
}
I will then obviously read the TempData variable in the controller.
I found another way.
In the controller you want to know what page it was called from.
I added the following in my controller
ViewBag.ReturnUrl = Request.UrlReferrer.AbsolutePath;
Then in the View I have a 'Back' button
#(Html.Kendo().Button().Name("ReturnButton")
.Content("Back to List").Events(e => e.Click("onReturn"))
.HtmlAttributes(new { type = "k-button" })
)
Then the javascript for the onReturn handler
function onReturn(e) {
var url = '#(ViewBag.ReturnUrl)';
window.location.href = url;
}

Passing Addition parameter to IControllerFactory.CreateController

I am using MEF to dynamically load controllers in an MVC3 app.
In the export metadata I am specifying two meta data constraints
EX:
[ExportMetadata("controllerName", "APSR")]
[ExportMetadata("controllerVersion", "1.0.0.0")]
In my "main" mvc app, I am using a RedirectToAction method (In reponse to a user click on a dropdown)
[HttpPost]
public ActionResult Index(Models.HomeViewModel selected)
{
//ViewData.Add("Version", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version);
return RedirectToAction("Create", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Controller);
}
How can I pass the desired version number to my Controller factory? Since the IControllerFactory.CreateController method only excepts to paramters:
IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
I would imagine you need some additional route data, and reading that when creating your controller.
For instance, I could define a route as:
routes.MapRoute(
"APSR_Create",
"/apsr/{version}/create",
new {
controller = "APSR",
action = "Create",
version = "1.0.0.0"
});
Now, when I create an instance of my controller, I can grab that version item from the RequestContext.RouteData collection:
public IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
string version = requestContext.RouteData["version"];
// Create instance using metadata lookup...
}
You just need to ensure that you are passing the version as an argument to the route.
return RedirectToAction(
"Create",
new { version = selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version });

Resources