how I can implement default product in each category in mvc4 using routing - asp.net-mvc-4

how I can implement default product in each category in mvc4 using routing?

You'll want to set up your routes like this:
routes.MapRoute("Category Route",
"categories/{category}/{product}",
new {
Controller = "Categories",
Action = "ShowCategory",
Category = "Books",
Product = ""
});
Then, your controller action method could look like this:
public CategoriesController : Controller {
public ActionResult ShowCategory(String category, String product) {
// rest of code goes here
}
}
Then inside your action you can design your repository to check String.IsNullOrEmpty(product) and determine the default one based on a database flag or however you want to indicate that.

Related

ASP.Net Core MVC - Calling a different view from a controller

I am new to ASP.Net Core MVC and have a question. I have a home controller which uses a index view to show a list of customers in a table. Against each row I have two buttons; an edit and delete button. When the user clicks on the edit button it calls an action in my home controller called editcustomer passing in the customer Id. In this action I create a view model and populate all the models with the correct data. I then need to call my customer details view with this view model to display all the customer information.
I am not sure how I switch from my home controller to my customer details controller and display my customer details view.
I am new to this and I am going through a ton of YouTube videos to study but I just need a bit of guidance on how to do this process correctly.
Below is the code for my EditCustomer action but it errors because it's looking for the view in the home (controller name) folder and not the CustomerDetails folder under the view folder:
public IActionResult EditCustomer(int customer_Id)
{
CustomerDetailsViewModel customerDetails = new CustomerDetailsViewModel();
customerDetails.customer = customerRepository.getCustomer(customer_Id);
customerDetails.titles = customerRepository.getTitles();
customerDetails.ePCs = customerRepository.getEPCs();
customerDetails.jobStages = customerRepository.getJobStages();
customerDetails.propertyStatus = customerRepository.getPropertyStatus();
customerDetails.propertyTypes = customerRepository.getPropertyTypes();
customerDetails.heatSources = customerRepository.getHeatSources();
customerDetails.fuelTypes = customerRepository.getFuelTypes();
customerDetails.ePCRequesters = customerRepository.getEPCRequesters();
customerDetails.ePCCompanies = customerRepository.getEPCCompanies();
customerDetails.surveyors = customerRepository.getSurveyors();
customerDetails.surveyStatus = customerRepository.getSurveyStatus();
customerDetails.installationInstallers = customerRepository.getInstallationInstallers();
customerDetails.loftInstallers = customerRepository.getLoftInstallers();
customerDetails.heatInstallers = customerRepository.getHeatInstallers();
return View("CustomerDetails",customerDetails);
}
You can try to use RedirectToAction,pass model to a action in another controller,and return View with the model.Here is a demo:
BController:
public class BController : Controller
{
public IActionResult TestRedirect()
{
return RedirectToAction("CustomerDetails", "Home", new CustomerDetailsViewModel { titles="titlesample"});
}
}
HomeController:
public class HomeController : Controller
{
public IActionResult CustomerDetails(CustomerDetailsViewModel c)
{
return View(c);
}
}
View(Home/CustomerDetails):
#model CustomerDetailsViewModel
<h1>
#Model.titles
</h1>
result:

Route to allow a parameter from both query string and default {id} template

I have an action in my ASP.Net Core WebAPI Controller which takes one parameter. I'm trying to configure it to be able to call it in following forms:
api/{controller}/{action}/{id}
api/{controller}/{action}?id={id}
I can't seem to get the routing right, as I can only make one form to be recognized. The (simplified) action signature looks like this: public ActionResult<string> Get(Guid id). These are the routes I've tried:
[HttpGet("Get")] -- mapped to api/MyController/Get?id=...
[HttpGet("Get/{id}")] -- mapped to api/MyController/Get/...
both of them -- mapped to api/MyController/Get/...
How can I configure my action to be called using both URL forms?
if you want to use route templates
you can provide one in Startup.cs Configure Method Like This:
app.UseMvc(o =>
{
o.MapRoute("main", "{controller}/{action}/{id?}");
});
now you can use both of request addresses.
If you want to use the attribute routing you can use the same way:
[HttpGet("Get/{id?}")]
public async ValueTask<IActionResult> Get(
Guid id)
{
return Ok(id);
}
Make the parameter optional
[Route("api/MyController")]
public class MyController: Controller {
//GET api/MyController/Get
//GET api/MyController/Get/{285A477F-22A7-4691-AA51-08247FB93F7E}
//GET api/MyController/Get?id={285A477F-22A7-4691-AA51-08247FB93F7E}
[HttpGet("Get/{id:guid?}"
public ActionResult<string> Get(Guid? id) {
if(id == null)
return BadRequest();
//...
}
}
This however means that you would need to do some validation of the parameter in the action to account for the fact that it can be passed in as null because of the action being able to accept api/MyController/Get on its own.
Reference Routing to controller actions in ASP.NET Core

How to get #Url.Action value inside a controller

I am using ASP.net core
I can use an Html action inside a view
#Url.Action("GetOptions", "ControllerName", new { id="1"});
However I want to get a string value of it in the Controller.
e.g. something like
string Url= Url.Action("GetOptions", "ControllerName", new { id="1"}).ToString();
In previous versions of MVC you can reference the helper in the controller by
UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Basically what I want to do is generate a URL string representation in my controller
In order for the route values to work correctly for me I had to use the static invocation of the url helpers
UrlHelperExtensions.Action(Url, "Details", "Resellers", new { id = 1 })
Edit: The shorthand way of writing this is:
this.Url.Action("Details", "Resellers", new { id = 1 })
Thanks #Learner.

Is it possible to render View without create Action in ASP.NET MVC?

Is it possible to get/render View without creating Action in Controller? I have many Views where I dont need pass any model or viewbag variables and I thing, its useless to create only empty Actions with names of my Views.
You could create a custom route, and handle it in a generic controller:
In your RouteConfig.cs:
routes.MapRoute(
"GenericRoute", // Route name
"Generic/{viewName}", // URL with parameters
new { controller = "Generic", action = "RenderView", }
);
And then implement a controller like this
public GenericContoller : ...
{
public ActionResult RenderView(string viewName)
{
// depending on where you store your routes perhaps you need
// to use the controller name to choose the rigth view
return View(viewName);
}
}
Then when a url like this is requested:
http://..../Generic/ViewName
The View, with the provided name will be rendered.
Of course, you can make variations of this idea to adapt it to your case. For example:
routes.MapRoute(
"GenericRoute", // Route name
"{controller}/{viewName}", // URL with parameters
new { action = "RenderView", }
);
In this case, all your controllers need to implement a RenderView, and the url is http://.../ControllerName/ViewName.
In my opinion it's not possible, the least you can create is
public ActionResult yourView()
{
return View();
}
If these views are partial views that are part of a view that corresponds to an action, you can use #Html.Partial in your main view to render the partial without an action.
For example:
#Html.Partial("MyPartialName")

ASP.NET Web API binding method

I have two methods like this
public class ProductController : ApiController
{
public Product GetProductById(int id)
{
var product = ... //get product
return product;
}
public Product GetProduct(int id)
{
var product = ... //get product
return product;
}
}
When I call url: GET http://localhost/api/product/1 . I want the first method is invoked, not the second method.
How can I do that ?
You need unique URIs. You can modify your route to get this:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Now you can access your API like this:
http://localhost/api/product/GetProductById/1
http://localhost/api/product/GetProduct/1
I've written a little introduction to ASP.NET Web API which shows some of the differences to WCF Web API.
You can also add a default action, e.g. the one the lists all products so you can do something like this:
http://localhost/api/product/ // returns the list without specifying the method
and the other one is invoked this way
http://localhost/api/product/byid/1 // returns the list without specifying the method
What I do is having a ProductsController and a ProductController.
ProductsController is responsible for operations on Collections of T (get all) and ProductController is responsible for operations on T (like getting a specific one).